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
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"

Avenging Dentist posted:

Or just, you know, use a set.

Adbot
ADBOT LOVES YOU

fritz
Jul 26, 2003

UnNethertrash posted:

If you have a list that you know is sorted, is there a built in way to efficiently search the list?

For example, say I have a list of primes under 1 million in ascending order, and I want to check a number to see if it's prime.

I'm not very good at programming, but I know how to write a binary search. However, I think that a) there are probably even faster ways to search, b) python would have something built in.

Thanks in advance.

The primes are more or less evenly distributed so you should be able to do better than binary search with some guessing and arithmetic.

Actually a million isn't all that big, just make a list of True and False and blammo you have an O(1) algorithm.

UnNethertrash
Jun 14, 2007
The Trashman Cometh

Avenging Dentist posted:

Or just, you know, use a set.

What do you mean? Set is unordered, it only prevents repeats of any item. Does it have a built it binary search or something?

Ninja edit: I think I figured it out, if it's in the set it's prime. Is this fast?

Fritz: Do you mean setting it up so you just basically...
code:
return list_O_primes[number_I_want_to_test]
That actually seems brilliantly simple to me.

Avenging Dentist
Oct 1, 2005

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

UnNethertrash posted:

What do you mean? Set is unordered, it only prevents repeats of any item. Does it have a built it binary search or something?

"Unordered" doesn't mean what you think it means. I'm pretty sure Python sets use a hash table.

king_kilr
May 25, 2007

Avenging Dentist posted:

"Unordered" doesn't mean what you think it means. I'm pretty sure Python sets use a hash table.

It does, it happens to be a very very fast hashset to boot. On simple numeric keyed hashsets I can often benchmark python as being faster than std::tr1::unoreded_set.

Avenging Dentist
Oct 1, 2005

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

king_kilr posted:

It does, it happens to be a very very fast hashset to boot. On simple numeric keyed hashsets I can often benchmark python as being faster than std::tr1::unoreded_set.

This is a stupid benchmark to make since there are already several completely separate implementations of unordered_set (or rather, it's a stupid assertion to make from a single benchmark).

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"

UnNethertrash posted:

What do you mean? Set is unordered, it only prevents repeats of any item. Does it have a built it binary search or something?

Read the bold part, and think about what it means regarding searches for set membership.

maskenfreiheit
Dec 30, 2004
...

maskenfreiheit fucked around with this message at 03:51 on Sep 29, 2010

tef
May 30, 2004

-> some l-system crap ->

king_kilr posted:

On simple numeric keyed hashsets I can often benchmark python as being faster than std::tr1::unoreded_set.


For integers hash(n) = n in the python dictionary implementation*

quote:

Major subtleties ahead: Most hash schemes depend on having a "good" hash
function, in the sense of simulating randomness. Python doesn't: its most
important hash functions (for strings and ints) are very regular in common
cases:

>>> map(hash, (0, 1, 2, 3))
[0, 1, 2, 3]


* Except hash(-1) = -2

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

GregNorc posted:

So I'm looking to learn python.

I have done a little scripting before, so I know what a loop is, what a variable is, etc. But usually what would happen is I'd pick up "Learning $language", get bored with stupid hello world BS and give up.

To rememdy this I've got two strategies.

First I'm using python. In addition to being pretty awesome, it's interpreted, so easier to get feedback.

Second, I designed my own little project: Making a game to teach people to count cards.

So I can start out with a simple command line blackjack program, add in all the extra stuff like doubling and splits, add in the card counting part, add in a gui, and pretty much learn most of the basic python syntax in the process.

I was just looking for ideas for books/sites to read for guidance when I get stuck.

Thanks.

The links in the op are still good. Dive Into Python (free online) and others are great resources.

king_kilr
May 25, 2007

tef posted:

For integers hash(n) = n in the python dictionary implementation*



* Except hash(-1) = -2

That's all true, however the speed of a hashtable implementation involves more than just the cost to hash something.

tef
May 30, 2004

-> some l-system crap ->
It's not the only thing I just thought it was interesting :shobon:

king_kilr
May 25, 2007

tef posted:

It's not the only thing I just thought it was interesting :shobon:

Actually anything that hashes to -1 returns -2 for it's hash:
code:
>>> class A(object):
...     def __hash__(self):
...         return -1
...     
...     

>>> hash(A())
[2] -2
My guess is -1 is used internally as a "this failed" so it just translates -1 -> -2, no harm no foul :)

maskenfreiheit
Dec 30, 2004
Edit: doublepost

maskenfreiheit fucked around with this message at 01:13 on Mar 13, 2017

king_kilr
May 25, 2007
I'd just do:

code:
def create_deck():
    return [(suit, value) for suit in ('Hearts', 'Clubs', 'Spades', 'Diamonds') for value in xrange(1, 14)]
:)

Or with nice whitespace:

code:
def create_deck():
    deck = []
    for suit in ('Hearts', 'Clubs', 'Spades', 'Diamonds'):
        for value in xrange(1, 14): # Ace (1) -> King (13)
            deck.append((suit, value))
    return deck

ATLbeer
Sep 26, 2004
Über nerd

king_kilr posted:

I'd just do:

code:
def create_deck():
    return [(suit, value) for suit in ('Hearts', 'Clubs', 'Spades', 'Diamonds') for value in xrange(1, 14)]
:)

code:
def create_deck():
    from random import shuffle
    deck = [(suit, value) for suit in ('Hearts', 'Clubs', 'Spades', 'Diamonds') for value in xrange(1, 14)]
    shuffle(deck)
    return deck

Soviet Space Dog
May 7, 2009
Unicum Space Dog
May 6, 2009

NOBODY WILL REALIZE MY POSTS ARE SHIT NOW THAT MY NAME IS PURPLE :smug:

GregNorc posted:

Ok, quick question: can I make a list of lists in python?

Let me explain...

I'm making a blackjack program as a little project to help learn programming.

I'm writing the first function, called createDeck(), which will as you can probably guess, create the deck of cards.

This is how I planned to do it: I have two counters, one called cardCount, rankCount, and suitCount.

So I have a loop that goes through all the possible combinations, and at the end should have produced 52 cards (cards just being lists with two values: the first is 0-3 to denote suit, the other is 0-13 to denote rank.

Here's the psudocode I wrote out (I'm not concerned with syntax stuff yet, just wanna get the logic down):

code:
def createDeck():
        cardCount = 0
        rankCount = 0
        suitCount = 0

        while cardCount >= 51
                cardList[cardCount] = (rankCount, suitCount);
                suitCount = suit + 1;
                rankCount = rank + 1;
                if suit = 3 and rank = 13
                        break;
                if suit > 13
                        suit = 0;
                if rank > 4
                        rank = 0;
Thoughts?

Use nested for loops. There is no reason to use a while loop since you are basically iterating through fixed sets.

tripwire
Nov 19, 2004

        ghost flow

Soviet Space Dog posted:

Use nested for loops. There is no reason to use a while loop since you are basically iterating through fixed sets.

Or even better (and more pythonic), use the list comprehension ATLBeer posted above

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah
Does anyone know if PIL supports the creation of animated GIFs?

I'm creating a small command line utility for a buddy that will input a small image, make it a two-frame animated GIF with one frame that has a single pixel with a slight coloration difference for import into Windows Live Messenger as an emoticon (the point of doing so prevents WLM from distorting the emoticon by automatically resizing it).

If PIL isn't the best imaging library for this sorta thing, any recommendations for something better would be great.

Scaevolus
Apr 16, 2007

root beer posted:

Does anyone know if PIL supports the creation of animated GIFs?

I'm creating a small command line utility for a buddy that will input a small image, make it a two-frame animated GIF with one frame that has a single pixel with a slight coloration difference for import into Windows Live Messenger as an emoticon (the point of doing so prevents WLM from distorting the emoticon by automatically resizing it).

If PIL isn't the best imaging library for this sorta thing, any recommendations for something better would be great.

A bit of searching led me to http://sites.google.com/site/almarklein/files-1/images2gif.py , which should show you what you want.

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

Scaevolus posted:

A bit of searching led me to http://sites.google.com/site/almarklein/files-1/images2gif.py , which should show you what you want.
Boy I feel dumb, I came across that very same thing and disregarded it for some reason I can't seem to recall. :downs:

Thanks!

ShizCakes
Jul 16, 2001
BANNED
I'm having a bit of trouble daemonizing a script.

I found a couple of examples online, and am using this one because I felt like I understood it:

http://www.enterpriseitplanet.com/networking/features/article.php/11315_3786386_1
code:
import sys, os
def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
    # Perform first fork.
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0) # Exit first parent.
    except OSError, e:
        sys.stderr.write("fork #1 failed: (%d) %sn" % (e.errno, e.strerror))
        sys.exit(1)
    # Decouple from parent environment.
    os.chdir("/")
    os.umask(0)
    os.setsid()
    # Perform second fork.
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0) # Exit second parent.
    except OSError, e:
        sys.stderr.write("fork #2 failed: (%d) %sn" % (e.errno, e.strerror))
        sys.exit(1)
    # The process is now daemonized, redirect standard file descriptors.
    for f in sys.stdout, sys.stderr: f.flush()
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())
Calling the above from my script properly causes the script to be daemonized. My problem is that when I modify stdout and stderr above to be something like /tmp/butts.log, the file is created - and no output is sent to it. The file remains 0 bytes.

Redirecting stdout to a file within my main script (when NOT daemonized) works fine. For example:
code:
sys.stdout = open("butts.log", "a")
When I call daemonize, the same thing happens - the file is created, but nothing is added to it.

Commenting out the entire block in the daemon script after "# The process is now daemonized" does not seem to affect whether or not stdout redirection is working within my main script when daemonized.

The script otherwise functions normally.

I'm really confused. I know nothing about how stdout and stderr work with filedescriptors, or the proper procedure for dealing with them. If it matters, this is on CentOS 5.3.

ShizCakes fucked around with this message at 19:51 on Sep 1, 2009

ShizCakes
Jul 16, 2001
BANNED

ShizCakes posted:

I'm having a bit of trouble daemonizing a script.

Calling the above from my script properly causes the script to be daemonized. My problem is that when I modify stdout and stderr above to be something like /tmp/butts.log, the file is created - and no output is sent to it. The file remains 0 bytes.

Redirecting stdout to a file within my main script (when NOT daemonized) works fine. For example:
code:
sys.stdout = open("butts.log", "a")
When I call daemonize, the same thing happens - the file is created, but nothing is added to it.

Commenting out the entire block in the daemon script after "# The process is now daemonized" does not seem to affect whether or not stdout redirection is working within my main script when daemonized.

The script otherwise functions normally.

I'm really confused. I know nothing about how stdout and stderr work with filedescriptors, or the proper procedure for dealing with them. If it matters, this is on CentOS 5.3.

After waiting a long time, I noticed that it was outputting things in 4K blocks. That lead me to suspect that there was buffering occuring of sys.stdout - and there was!

Not sure why it was happening when daemonized but seemingly not when not daemonized, but the answer to my woes was found here:

http://stackoverflow.com/questions/107705/python-output-buffering

bitprophet
Jul 22, 2004
Taco Defender

ShizCakes posted:

After waiting a long time, I noticed that it was outputting things in 4K blocks. That lead me to suspect that there was buffering occuring of sys.stdout - and there was!

Not sure why it was happening when daemonized but seemingly not when not daemonized, but the answer to my woes was found here:

http://stackoverflow.com/questions/107705/python-output-buffering

Yup, stdout buffering is one of those things you inevitably run into and then (hopefully) never forget about afterwards. I sort of wish it was highlighted more in tutorials, aside from the "learn to program!" tutorials where it would be a bit much.

In these situations I often find myself just making a stupid little subroutine that does the flushing; outright replacing sys.stdout like in that SO thread is a more complete solution, at the expense of loving around with internals (i.e. and possibly surprising coders who come after you and aren't expecting it.) It's sometimes necessary, though :)

ATLbeer
Sep 26, 2004
Über nerd
code:
null-0023322e1568:Jinja2-2.1.1 ag$ python setup.py clean
running clean
removing 'build/temp.macosx-10.3-i386-2.5' (and everything under it)
null-0023322e1568:Jinja2-2.1.1 ag$ python setup.py build
running build
running build_py
running build_ext
building 'jinja2._speedups' extension
creating build/temp.macosx-10.3-i386-2.5
creating build/temp.macosx-10.3-i386-2.5/jinja2
gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c jinja2/_speedups.c -o build/temp.macosx-10.3-i386-2.5/jinja2/_speedups.o
cc1: error: unrecognized command line option "-Wno-long-double"
cc1: error: unrecognized command line option "-Wno-long-double"
lipo: can't figure out the architecture type of: /var/folders/fg/fgp4uHqFH7G2xxpI5rZskE+++TI/-Tmp-//ccF4iEOX.out
error: command 'gcc' failed with exit status 1

Hmmm... I had jinja2 installed and working on Leopard but, after my Snow Leopard upgrade it didn't exist and now I can't seem to build it.

Any pointers?

(Also just checked out latest version from jinja's mercurial and get the same error)

eh... oh well: python setup.py --without-speedups install

that works

OK... This is now a reoccurring problem. Seems like Snow Leopard and distutils might not get along

Fabric just failed in the exact same way (with installing pycrypto)

ATLbeer fucked around with this message at 22:31 on Sep 2, 2009

good jovi
Dec 11, 2000

You need to reinstall XCode after installing Snow Leopard.

EDIT: upon actually reading your code, maybe not. it looks like you have gcc, but it just isn't working. Still, if you haven't, do so.

ATLbeer
Sep 26, 2004
Über nerd

Sailor_Spoon posted:

You need to reinstall XCode after installing Snow Leopard.

EDIT: upon actually reading your code, maybe not. it looks like you have gcc, but it just isn't working. Still, if you haven't, do so.

Yeah.... I reinstalled XCode, then reinstalled it w/ 10.4 support... Dunno why

Neither made a difference

Xenos
Jun 17, 2005

ATLbeer posted:

Hmmm... I had jinja2 installed and working on Leopard but, after my Snow Leopard upgrade it didn't exist and now I can't seem to build it.

Any pointers?

I haven't used Snow Leopard yet, but I'm going to guess that it has a 64-bit build of Python. Try this before installing:

code:
export ARCHFLAGS='-arch i386 -arch x86_64'

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

ATLbeer posted:

Yeah.... I reinstalled XCode, then reinstalled it w/ 10.4 support... Dunno why

Neither made a difference

You're using the default python2.6.1 installation, right? Not something left over from installing python2.6 from python.org? I had no problems pulling in Jinja2, which is a dep of sphinx

ATLbeer
Sep 26, 2004
Über nerd

m0nk3yz posted:

You're using the default python2.6.1 installation, right? Not something left over from installing python2.6 from python.org? I had no problems pulling in Jinja2, which is a dep of sphinx

You just made me double check... My work Mac I have 2.5 installed and on my home laptop I have 2.6.1

Both fail in the exact same way.

code:
jinja2/_speedups.c:207: error: ‘NULL’ undeclared here (not in a function)
lipo: can't figure out the architecture type of: /var/folders/By/ByGfXc-8E4ioG4y175OkS++++TI/-Tmp-//ccVCjqFs.out
error: Setup script exited with error: command 'gcc' failed with exit status 1

ATLbeer
Sep 26, 2004
Über nerd

Xenos posted:

I haven't used Snow Leopard yet, but I'm going to guess that it has a 64-bit build of Python. Try this before installing:

code:
export ARCHFLAGS='-arch i386 -arch x86_64'

:(

code:
src/hash_template.c:221: error: (near initialization for ‘ALG_functions[1].ml_meth’)
lipo: can't figure out the architecture type of: /var/folders/By/ByGfXc-8E4ioG4y175OkS++++TI/-Tmp-//ccPFZu5P.out
error: Setup script exited with error: command 'gcc' failed with exit status 1
axx$ export | grep AR
declare -x ARCHFLAGS="-arch i386 -arch x86_64"
axx:~ a$ 

ATLbeer fucked around with this message at 04:00 on Sep 3, 2009

Xenos
Jun 17, 2005

ATLbeer posted:

:(

Are you trying to install as sudo? If so, pass it through like this:

code:
sudo env ARCHFLAGS="-arch i386 -arch x86_64" <your install command>
I don't really want to keep guessing, though. I'll try to install Jinja2 on my work machine in the morning, and let you know if I come up with anything.

ATLbeer
Sep 26, 2004
Über nerd

Xenos posted:

Are you trying to install as sudo? If so, pass it through like this:

code:
sudo env ARCHFLAGS="-arch i386 -arch x86_64" <your install command>
I don't really want to keep guessing, though. I'll try to install Jinja2 on my work machine in the morning, and let you know if I come up with anything.

http://pastebin.com/f6c96dcfa

And I do have XCode installed, latest version I believe [3.2 (1610)]

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

ATLbeer posted:

http://pastebin.com/f6c96dcfa

And I do have XCode installed, latest version I believe [3.2 (1610)]

I can confirm that on Snow Leopard, fresh install - no ARCHFLAGS set and the xcode that ships with leopard, it installs OK:

code:
bot:~ jesse$ mkvirtualenv jinja-test
New python executable in jinja-test/bin/python
Installing setuptools............done.
(jinja-test)bot:~ jesse$ cd ~/Downloads/
(jinja-test)bot:Downloads jesse$ tar -xzf Jinja2-2.1.1.tar.gz 
(jinja-test)bot:Downloads jesse$ cd Jinja2-2.1.1
(jinja-test)bot:Jinja2-2.1.1 jesse$ python setup.py install
running install
running bdist_egg
running egg_info
writing requirements to Jinja2.egg-info/requires.txt
writing Jinja2.egg-info/PKG-INFO
writing top-level names to Jinja2.egg-info/top_level.txt
writing dependency_links to Jinja2.egg-info/dependency_links.txt
writing entry points to Jinja2.egg-info/entry_points.txt
reading manifest file 'Jinja2.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'Makefile'
warning: no files found matching 'ez_setup.py'
warning: no previously-included files matching '*' found under directory 'docs/_build/doctrees'
writing manifest file 'Jinja2.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.6-universal/egg
running install_lib
running build_py
creating build
...snip...
Processing Jinja2-2.1.1-py2.6-macosx-10.6-universal.egg
creating /Users/jesse/.virtualenvs/jinja-test/lib/python2.6/site-packages/Jinja2-2.1.1-py2.6-macosx-10.6-universal.egg
Extracting Jinja2-2.1.1-py2.6-macosx-10.6-universal.egg to /Users/jesse/.virtualenvs/jinja-test/lib/python2.6/site-packages
Adding Jinja2 2.1.1 to easy-install.pth file

Installed /Users/jesse/.virtualenvs/jinja-test/lib/python2.6/site-packages/Jinja2-2.1.1-py2.6-macosx-10.6-universal.egg
Processing dependencies for Jinja2==2.1.1
Finished processing dependencies for Jinja2==2.1.1
(jinja-test)bot:Jinja2-2.1.1 jesse$ 
You should be pointing at /usr/bin/python - also, do you have MACOSX_DEPLOYMENT_TARGET= exported in your environment at all?

ATLbeer
Sep 26, 2004
Über nerd

m0nk3yz posted:

I can confirm that on Snow Leopard, fresh install - no ARCHFLAGS set and the xcode that ships with leopard, it installs OK:

[code]
You should be pointing at /usr/bin/python - also, do you have MACOSX_DEPLOYMENT_TARGET= exported in your environment at all?
$ which python
/Library/Frameworks/Python.framework/Versions/Current/bin/python
$ export | grep MACOSX

Is this this the default config?

The follow up I guess is how do I regress to the standard install without blowing away my install

ATLbeer fucked around with this message at 16:37 on Sep 3, 2009

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

ATLbeer posted:

$ which python
/Library/Frameworks/Python.framework/Versions/Current/bin/python
$ export | grep MACOSX

Is this this the default config?

The follow up I guess is how do I regress to the standard install without blowing away my install

code:
bot:~ jesse$ which python
/usr/bin/python
bot:~ jesse$ ls -lah /usr/bin/python
-rwxr-xr-x  2 root  wheel    84K Jul  8 02:57 /usr/bin/python
bot:~ jesse$ 
Do you have your $PATH set to /Library/Frameworks/Python.framework/Versions/Current/bin? I think the python.org tweaks your path - check your .bash_profile/.bashrc

In order to reset it, I think all you need to do is remove any custom $PATH settings and make sure you're pulling in /usr/bin python.

In fact - try that with the Jinja2 install - "/usr/bin/python setup.py install"

duneriver
Mar 20, 2009

by Fistgrrl
Hi, beginner coder here so any help would be much appreciated for Python 3.1.1:

#A simple program.
print ("Mary had a little lamb,")
print ("it's fleece was white as snow;")
print ("and everywhere that Mary went")
print ("her lamb was sure to go.")

Output:

Mary had a little lamb,
it's fleece was white as snow;
and everywhere that Mary went
her lamb was sure to go.

------------

In the above, how do I recode so the last 2 lines run into each other without a line break? I remember it involving '\' somehow, but I can't get it to work with the 'print' function.

Also, is there any important difference between using ' and " to enclose strings?

duneriver fucked around with this message at 10:38 on Sep 4, 2009

deimos
Nov 30, 2006

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

duneriver posted:

In the above, how do I recode so the last 2 lines run into each other without a line break? I remember it involving '\' somehow, but I can't get it to work with the 'print' function.

print("string", end="")

duneriver posted:

Also, is there any important difference between using ' and " to enclose strings?

' lets you embed " in them and " lets you embed ' in them!

duneriver
Mar 20, 2009

by Fistgrrl
Thank you!

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->
I made some stupid decorators:

code:
import inspect
import ast

def unparsed(f):
    return "".join(inspect.getsourcelines(f)[0][1:])

def uncompiled(f):
    return compile(unparsed(f),f.__code__.co_filename,'exec',ast.PyCF_ONLY_AST)

@unparsed
def id(x):
    return x

@uncompiled
def en(x):
    return x

print id
print ast.dump(en)
:v:

  • Locked thread