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
tef
May 30, 2004

-> some l-system crap ->

Stabby McDamage posted:

I just noticed that I can do "a not in b" just as well as "not a in b". That's cool, but weird to me. "not" can modify "in", but not other comparators...I can't say "a not > b". Is there a name for this behavior?

Essentially, there is an "in" operator, and a "not in" operator. There are also "is" and "is not" operators.

(although a similar thing is the notion of 'adverb methods' in ruby http://weblog.raganwald.com/2008/07/that-not-is-not-not-i-meant.html )

Adbot
ADBOT LOVES YOU

Stabby McDamage
Dec 11, 2005

Doctor Rope

tripwire posted:

not doesn't modify an operator, it modifies a boolean.

>>> not True
False

I know that, but that doesn't explain "a not in b", because by your definition "in b" is a boolean being modified by "not".

tef posted:

Essentially, there is an "in" operator, and a "not in" operator. There are also "is" and "is not" operators.

(although a similar thing is the notion of 'adverb methods' in ruby http://weblog.raganwald.com/2008/07/that-not-is-not-not-i-meant.html )

That explains it.

I wonder if "not in" is its own token in the Python lexer?

Allie
Jan 17, 2004

"not in" is an operator itself.

http://docs.python.org/reference/expressions.html#notin

Thermopyle
Jul 1, 2003

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

haywire posted:

Has anyone checked out gevent or denied yet? I've been playing around with Denied, it seems very nice - it isn't in that extensive server benchmark yet, but it's been getting people talking.

Also need to start playing around with thrift and cassandra and couchdb.

For those wanting to start using Gevent, I wrote a super simple "Hello World" app, that runs in separate greenlets:

code:
import gevent

def hello( name ):
        return "Hello, %s!" % name

if __name__ == '__main__':
        names = [ "James", "Ruth" ]
        jobs = [ gevent.spawn( hello, name ) for name in names ]
        gevent.joinall( jobs )
        for job in jobs:
                print job.value

I've been messing around with Eventlet (AFAICT, comprable to gevent) and really like it.

Khorne
May 1, 2002
What is the best python equivalent of:

someVar = !someVar

Google is just turning up a lot of things that aren't helping. They either seem excessively long or not applicable to the question.

tripwire
Nov 19, 2004

        ghost flow

Khorne posted:

What is the best python equivalent of:

someVar = !someVar

Google is just turning up a lot of things that aren't helping. They either seem excessively long or not applicable to the question.

someVar = not someVar

UberJumper
May 20, 2007
woop
Does anyone know what can be causing duplicate logging messages to be outputted?

code:
[DEBUG   ] 20100402 21:01:07 | auxpy.util.gen_generator | Interface Found
DEBUG:auxpy.util.gen_generator:Interface Found
In the auxpy.__init__.py i declare the root logger as

code:
_logger = logging.getLogger(__name__)
_logger.addHandler(NullHandler())
then i have a simple test file that changes this value into a stream logger for debugging.

Ive checked and all the loggers use, __name__ to get the logger.

Is there anyway to check this?

BeefofAges
Jun 5, 2004

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

UberJumper posted:

Does anyone know what can be causing duplicate logging messages to be outputted?

code:
[DEBUG   ] 20100402 21:01:07 | auxpy.util.gen_generator | Interface Found
DEBUG:auxpy.util.gen_generator:Interface Found
In the auxpy.__init__.py i declare the root logger as

code:
_logger = logging.getLogger(__name__)
_logger.addHandler(NullHandler())
then i have a simple test file that changes this value into a stream logger for debugging.

Ive checked and all the loggers use, __name__ to get the logger.

Is there anyway to check this?

Try checking how many handlers that logger has. Each handler will do its own log output. You can try printing _logger.handlers

UberJumper
May 20, 2007
woop

BeefofAges posted:

Try checking how many handlers that logger has. Each handler will do its own log output. You can try printing _logger.handlers

Bah. i found it, i was calling logging.debug instead of _logger.debug. Why would they make logging like that :psyduck:

Crazak P
Apr 11, 2003

PUNISHER > SPIDERMAN
I haven't had this much fun programming in awhile.

I've got a basic web app going where it'll list directories and files.

If you want to download whole directories, it zips it up and gives it to you. I've made it so it only zips the files in the immediate directory and not include any sub-directories. I don't want anyone to go zipping my whole music directory by accident.

I've also added a flash mp3 streamer that checks each directory to see if there are mp3 files, if there are, it'll generate a playlist for the streamer. Then embed the player into the page with the file list.

It's all very hastily put together, but it works. I'll probably start the bittorrent stuff next.

I know I'm probably doing things inefficiently or improperly so I'll probably put the code up some where to get some feedback.

Crazak P fucked around with this message at 10:20 on Apr 3, 2010

Ghetto Wizard
Aug 11, 2007
I've been trying to install lxml properly for 3 days now and I really can just not make it work properly. I keep getting this error:

code:
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python2.6/site-packages/lxml-2.2.5-py2.6-linux-i686.egg/lxml/etree.so: symbol xmlSchematronSetValidStructuredErrors,
version LIBXML2_2.6.32 not defined in file libxml2.so.2 with link time reference
libxml2 and libxslt are both installed and so are the dev packages, aswell as the python dev package. I used easy_install to install lxml. I've downgraded to earlier versions of lxml and I still get the same error. I don't know what to do really.

edit: oh right, I use Debian 4.0

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
Try using pip, it's less retarded than easy_install.

Ghetto Wizard
Aug 11, 2007

deimos posted:

Try using pip, it's less retarded than easy_install.

I got rid of the egg easy_install created and then I installed pip, and then tried installing lxml again and tried testing lxml, which gave me the same error as the one before. So I uninstalled lxml again and installed Cython because I was getting warnings about etree needing to be present and without Cython pip wouldn't automatically create it. So I did that, then I installed lxml - Same error.

I do agree though, pip is less retarded than easy_install. It still doesn't work though.

Ghetto Wizard fucked around with this message at 17:30 on Apr 4, 2010

tripwire
Nov 19, 2004

        ghost flow

Ghetto Wizard posted:

I've been trying to install lxml properly for 3 days now and I really can just not make it work properly. I keep getting this error:

code:
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python2.6/site-packages/lxml-2.2.5-py2.6-linux-i686.egg/lxml/etree.so: symbol xmlSchematronSetValidStructuredErrors,
version LIBXML2_2.6.32 not defined in file libxml2.so.2 with link time reference
libxml2 and libxslt are both installed and so are the dev packages, aswell as the python dev package. I used easy_install to install lxml. I've downgraded to earlier versions of lxml and I still get the same error. I don't know what to do really.

edit: oh right, I use Debian 4.0
Are you installing lxml from the debian package repo or are you building from source?

Ghetto Wizard
Aug 11, 2007

tripwire posted:

Are you installing lxml from the debian package repo or are you building from source?

Pip finds packages automatically and builds them using that, but I'm not manually building it from source, though it is apparently possible.

Here's the pip install process of lxml:
code:
Downloading/unpacking lxml
  Downloading lxml-2.2.6.tar.gz (2.9Mb): 2.9Mb downloaded
  Running setup.py egg_info for package lxml
    Building lxml version 2.2.6.
    Building with Cython 0.12.1.
    Using build configuration of libxslt 1.1.26
    Building against libxml2/libxslt in the following directory: /usr/local/lib
    warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
  Running setup.py install for lxml
    Building lxml version 2.2.6.
    Building with Cython 0.12.1.
    Using build configuration of libxslt 1.1.26
    Building against libxml2/libxslt in the following directory: /usr/local/lib
    building 'lxml.etree' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include
 -I/usr/local/include/libxml2 -I/usr/local/include/python2.6 -c src/lxml/lxml.etree.c
 -o build/temp.linux-i686-2.6/src/lxml/lxml.etree.o -w
    gcc -pthread -shared build/temp.linux-i686-2.6/src/lxml/lxml.etree.o -L/usr/local/lib -lxslt -lexslt 
-lxml2 -lz -lm -o build/lib.linux-i686-2.6/lxml/etree.so
    building 'lxml.objectify' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include
 -I/usr/local/include/libxml2 -I/usr/local/include/python2.6 -c src/lxml/lxml.objectify.c 
-o build/temp.linux-i686-2.6/src/lxml/lxml.objectify.o -w
    gcc -pthread -shared build/temp.linux-i686-2.6/src/lxml/lxml.objectify.o -L/usr/local/lib -lxslt -lexslt -lxml2 -lz -lm 
-o build/lib.linux-i686-2.6/lxml/objectify.so
Successfully installed lxml
It's really easy to break tables using code :(

Ghetto Wizard fucked around with this message at 18:07 on Apr 4, 2010

tripwire
Nov 19, 2004

        ghost flow

Ghetto Wizard posted:

Pip finds packages automatically and builds them using that, but I'm not manually building it from source, though it is apparently possible.

Here's the pip install process of lxml:
code:
Downloading/unpacking lxml
  Downloading lxml-2.2.6.tar.gz (2.9Mb): 2.9Mb downloaded
  Running setup.py egg_info for package lxml
    Building lxml version 2.2.6.
    Building with Cython 0.12.1.
    Using build configuration of libxslt 1.1.26
    Building against libxml2/libxslt in the following directory: /usr/local/lib
    warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
  Running setup.py install for lxml
    Building lxml version 2.2.6.
    Building with Cython 0.12.1.
    Using build configuration of libxslt 1.1.26
    Building against libxml2/libxslt in the following directory: /usr/local/lib
    building 'lxml.etree' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include
 -I/usr/local/include/libxml2 -I/usr/local/include/python2.6 -c src/lxml/lxml.etree.c
 -o build/temp.linux-i686-2.6/src/lxml/lxml.etree.o -w
    gcc -pthread -shared build/temp.linux-i686-2.6/src/lxml/lxml.etree.o -L/usr/local/lib -lxslt -lexslt 
-lxml2 -lz -lm -o build/lib.linux-i686-2.6/lxml/etree.so
    building 'lxml.objectify' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include
 -I/usr/local/include/libxml2 -I/usr/local/include/python2.6 -c src/lxml/lxml.objectify.c 
-o build/temp.linux-i686-2.6/src/lxml/lxml.objectify.o -w
    gcc -pthread -shared build/temp.linux-i686-2.6/src/lxml/lxml.objectify.o -L/usr/local/lib -lxslt -lexslt -lxml2 -lz -lm 
-o build/lib.linux-i686-2.6/lxml/objectify.so
Successfully installed lxml
It's really easy to break tables using code :(
It looks like some kind of linking problem. The easiest thing to do would be to uninstall it and then install prebuilt binaries directly from the debian package repo: http://packages.debian.org/unstable/python/python-lxml
If that doesn't work maybe try the stable version from lenny (2.1.1)?

mystes
May 31, 2006

Ghetto Wizard posted:

I've been trying to install lxml properly for 3 days now and I really can just not make it work properly. I keep getting this error:

code:
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python2.6/site-packages/lxml-2.2.5-py2.6-linux-i686.egg/lxml/etree.so: symbol xmlSchematronSetValidStructuredErrors,
version LIBXML2_2.6.32 not defined in file libxml2.so.2 with link time reference
libxml2 and libxslt are both installed and so are the dev packages, aswell as the python dev package. I used easy_install to install lxml. I've downgraded to earlier versions of lxml and I still get the same error. I don't know what to do really.

edit: oh right, I use Debian 4.0
Install it through your your preferred Debian package manager.

Stabby McDamage
Dec 11, 2005

Doctor Rope
I have a module that uses telnetlib to talk to Cisco MDS switches. I need to add support for SSH now. However, Cisco's SSH implementation sucks -- it closes the SSH channel after every write, and I need execute a sequence of commands that have state, so connecting once per command is out.

I'm thinking of using pexpect, but before I get my hands dirty, is this the best way to go about this?

Given that I already have telnetlib working, I'm wondering if there's a shim layer or recipe out there for adding SSH support in a simple way.

EDIT: I solved it...wasn't that hard. I wrote a shim class that inherits pexpect but provides the functions that I care about from telnetlib, so I can just use it as a drop-in replacement. In case anyone cares:
code:
class SSHExpect (pexpect.spawn):
	"""
	Implements telnetlib.Telnet's functions (well, the ones I care about), 
	so that openssh+pexpect can be used as a drop-in replacement for telnetlib.
	
	Inherits pexpect, so all those methods work.  Instantiate with SSHExpect.connect(hostname, [username])
	"""
	@classmethod
	def connect(self,hostname,username=None,port=None,timeout=5):
		command = "ssh %s" % hostname
		if username: command += " -l%s" % username
		if port:     command += " -p%d" % port
		self = SSHExpect(command,timeout=timeout)
		return self
	
	def read_until(self, expected, timeout=-1):
		r = self.expect_exact(expected, timeout=timeout)
		return self.before + self.after

	def set_debuglevel(self, n):
		if n==1: self.logfile_read = sys.stdout
		if n>=2: self.logfile      = sys.stdout
		else: 
			self.logfile = None
			self.logfile_read = None

Stabby McDamage fucked around with this message at 23:28 on Apr 5, 2010

Spime Wrangler
Feb 23, 2003

Because we can.

I'm reading a bunch of values from a file using ConfigParser, and when converting from str to float there's a really small error being added to the values ('0.1' becomes 0.10000000000000001). Is there a standard way of avoiding this?

code:
       cfg = ConfigParser()
        cfg.read(config_filename)

        #Define product design variables
        self.prod_des_vars = cfg.get('product','des_vars').split(',')

        for var in self.prod_des_vars:
            bnds = cfg.get('product',var)
            lb = bnds.split(',')[0]
            ub = bnds.split(',')[1]

            #check validity of bounds
            if float(lb) > float(ub):
                print var,'--> bad bounds: ',lb,ub

            self.prod_des_var_bnds[0].extend([float(lb)])
            self.prod_des_var_bnds[1].extend([float(ub)])
also if there's anything else egregious in there please point it out

Avenging Dentist
Oct 1, 2005

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

Spime Wrangler posted:

Is there a standard way of avoiding this?

Stop using floats.

Spime Wrangler
Feb 23, 2003

Because we can.

Then what should I be using if the numbers aren't integers?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Since you haven't told us why this precision matters, I couldn't say. (It's going to boil down to fixed-point or arbitrary precision though. Or changing your expectations.)

Spime Wrangler
Feb 23, 2003

Because we can.

For purely functional reasons, it doesn't really. Just annoying when values are displayed everywhere to ridiculous precisions for no apparent reason. It just seemed like there should be a way to go from a string to a decimal value without something like this happening. Nevermind, I guess.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Then your problem is the precision that you use to print values. If you're actually printing the string version of the float it won't do this (it will when you print the repr of a float for obvious reasons). You can also manually specify precision with the string format specifiers.

By way of example:
code:
>>> print repr(0.1)
0.10000000000000001
>>> print str(0.1)
0.1
>>> 0.1
0.10000000000000001
>>> print 0.1
0.1

Spime Wrangler
Feb 23, 2003

Because we can.

Awesome. Thanks.

BeefofAges
Jun 5, 2004

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

You could also just round the numbers off.

http://function.name/in/Python/round/

Avenging Dentist
Oct 1, 2005

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

BeefofAges posted:

You could also just round the numbers off.

http://function.name/in/Python/round/

:ughh:

Scaevolus
Apr 16, 2007

Decimal module?

BeefofAges
Jun 5, 2004

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

Avenging Dentist posted:

:ughh:

I suppose I should have clarified that as rounding them before printing. It's possible that he could end up with some number that wasn't nice and round, like .387453876583456 or whatever, and want to round it to just a few digits. Of course, since he's taking values in from a config file, that isn't all that likely. I don't know where I'm going with this. Whatever.

blorpy
Jan 5, 2005

BeefofAges posted:

I suppose I should have clarified that as rounding them before printing. It's possible that he could end up with some number that wasn't nice and round, like .387453876583456 or whatever, and want to round it to just a few digits. Of course, since he's taking values in from a config file, that isn't all that likely. I don't know where I'm going with this. Whatever.

Avenging Dentist posted:

:ughh:

A A 2 3 5 8 K
Nov 24, 2003
Illiteracy... what does that word even mean?

BeefofAges posted:

I suppose I should have clarified that as rounding them before printing. It's possible that he could end up with some number that wasn't nice and round, like .387453876583456 or whatever, and want to round it to just a few digits. Of course, since he's taking values in from a config file, that isn't all that likely. I don't know where I'm going with this. Whatever.

>>> round(0.1, 3)
0.10000000000000001

BeefofAges
Jun 5, 2004

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

shhhhh


Here's where instead of admitting I was wrong I keep making excuses for the next page and a half:

Stabby McDamage
Dec 11, 2005

Doctor Rope
I'm surprised nobody's posted the underlying problem.

It's common to all floating-point arithmetic. Basically, as with integers, the number is stored using a binary representation. The form is:

      mantissa × 2exponent

Where mantissa and exponent are integers. Negative numbers are those with negative mantissas, numbers less than 1 are expressed with negative exponents.

Just as you can't represent 1/3 as a non-repeating decimal in base 10, you can't represent 1/10 as a non-repeating value in base 2. That's why you get small errors when the value is converted back to base 10 for printing. There are a heap of other caveats with using floating point math, too.

In many cases, fixed point math can be used to sidestep this if you're doing simple operations. For example, you can store an integer number of pennies instead of a floating point number of dollars.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
His problem wasn't the precision but the display. Your explanation is fine, but it really doesn't help since he doesn't care about the precision.

To just print the number up to 3 decimal places use something like this:
code:
"%.3f" % .1

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Aaaaaaaaaa gently caress you NumPy
code:
>>> A = numpy.arange(5)
>>> A[0:4] = A[3::-1]
>>> A
array([3, 2, 2, 3, 4])
Should be [3, 2, 1, 0, 4]

Modern Pragmatist
Aug 20, 2008

Avenging Dentist posted:

Aaaaaaaaaa gently caress you NumPy
code:
>>> A = numpy.arange(5)
>>> A[0:4] = A[3::-1]
>>> A
array([3, 2, 2, 3, 4])
Should be [3, 2, 1, 0, 4]

I guess they don't make a copy of a before referencing. Is the best solution:

code:
A[0:4] = A.copy()[3::-1]

Avenging Dentist
Oct 1, 2005

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

Modern Pragmatist posted:

I guess they don't make a copy of a before referencing.

I know why it's that way but it's beyond retarded that they don't check for it in the assignment, especially since it's trivially easy (A[3::-1].base is A).

Modern Pragmatist posted:

Is the best solution:

code:
A[0:4] = A.copy()[3::-1]

Actually it'd be better to put the copy at the end so you aren't copying the whole array.

Mostly I was just complaining though.

king_kilr
May 25, 2007

Avenging Dentist posted:

I know why it's that way but it's beyond retarded that they don't check for it in the assignment, especially since it's trivially easy (A[3::-1].base is A).


Actually it'd be better to put the copy at the end so you aren't copying the whole array.

Mostly I was just complaining though.

So file a bug, or ask on their mailing list. It may be that it's intentional.

Avenging Dentist
Oct 1, 2005

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

king_kilr posted:

So file a bug, or ask on their mailing list. It may be that it's intentional.

Actually I'm probably going to write a patch for it. It's just stupid that something like that slipped through.

Adbot
ADBOT LOVES YOU

UberJumper
May 20, 2007
woop
Okay so i am more or less wondering about something.

For my final Geomatics project, we were tasked with basically creating something to add functionality to the ArcGIS tool suite, or improve on something. E.g. basically ether make a cool tool or write a library. I ended up going a completely different route and basically have been working on porting making ArcObjects work decently from within python through comtypes. (i've done alot of arcobjects programming and using their geoprocessor pains me greatly).

However since ESRI's COM library is buggy as gently caress / broken / :suicide: for certain things. Comtypes runs into a lot of issues with properly generating modules for ArcObjects, e.g. iterable interfaces are not being treated as iterable, even though ESRI says they should be. So its a pretty big mishmash of stuff. Now to top it off, comtypes is not very customizable, and basically doesn't allow you to externally modify certain things. So what i was wondering, is it a bad pratice to basically copy the comtypes package, rename it so something like _comtypes, update all references within that package and merge it into a larger package? Then just edit the living hell out of it directly, and make it more or less work only for ArcObjects?

A few people have approached or messaged me about being interested in getting a copy of when i get it into a workable state. So i was thinking of just putting it up on google code or codeplex. I have tried contacting the comtypes dev's a few times about asking them if i can just modify comtypes. I only got one response, which was basically "uhmm no". My duedate is coming up, and i pretty much am just going to go ahead with it.

So basically what i am asking is, if i did the above would:
a. comtypes people get pissy, and can they basically tell me to take it down?
b. I am not giving out the com type libs, since you need a valid arc install to generate the bindings. Does this really count as reverse engineering?
c. In the TOS ESRI States that you cannot use ArcObjects from any non ESRI approved
languages. However ESRI gives users a python library, just a very limited one.

Is there anything i really should be weary of doing?

  • Locked thread