Re: IP address

2007-01-28 Thread Beej
On Jan 28, 2:26 am, Klaus Alexander Seistrup <[EMAIL PROTECTED]> wrote:
> Scripter47 wrote:
> > How do i get my ip address?
>
> > in cmd.exe i just type "ipconfig" then it prints:
> >   ...
> >   IP-address . . . . . . . . . . . . . . . . . : 192.168.1.10
> >   ...
> > how can i do that in python??#v+
>
> python -c 'import re, urllib; print re.findall("Your IP: 
> (.+?)", urllib.urlopen("http://myip.dk/";).read())[0]'

This is extremely unlikely to return 192.168.1.10. :-)

(It will give you the address of your firewall or whatever is your 
gateway to the outside world...  which is a cool thing to know, but 
I'm not sure it's what the op's after.)

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data structure and algorithms

2007-01-29 Thread Beej
On Jan 28, 2:56 pm, "azrael" <[EMAIL PROTECTED]> wrote:
> class Node:
>   def __init__(self, cargo=None, next=None):
> self.cargo = cargo
> self.next  = next

This is OK for the node itself, but maybe you should try writing a 
LinkedList class that you use:

class LinkedList(object):
def __init__(self):
self.head = None

def append(self, node):
...

def remove(self, node):
...

>>> ll = LinkedList()
>>> ll.append(Node(3490))

(For extra fun, make it so you can iterate over the linked list and 
call it like this:for n in ll: print n   :-) )

> >>> list=[]
> >>> list.append(Node(1))
> >>> list.append(Node(2))
> >>> list[0].next=list[1]
> >>> list.append(Node(3))
> >>> list[1].next=list[2]

I'd classify this as "not pretty".  Sure, it's more "dynamic" in that 
you don't have a variable to reference every node, but it's way 
cleaner to make an encapsulating LinkedList class, right?

In in Python, references to objects are very much like pointers in C:

>>> foo = Node(3490)  # foo is a reference to a Node instance
>>> bar = foo   # bar points to the same Node instance as foo
>>> foo
<__main__.Node object at 0xb7b362ec>
>>> bar
<__main__.Node object at 0xb7b362ec>

See?  They point to the name Node object.

> I think that my concept is wrong by using a list to create a list.

I agree with you, if your goal is to implement your own list.  Using 
the Python functionality just makes things less clear.

> Is
> it possible to manage the insertation of new object like in C by
> allocating new memory space.

Absolutely.  The "next" pointer thing is the way to go, so you're on 
the right track with that.

When deleting nodes from the list, you don't explicitly delete them; 
you just need to remove all your references to them.  Nodes will be 
garbage collected when there are no more references to them anywhere.

> any sugestions how to make the implementation more like in C. I never
> managed the syntax of C so I stoped when structs crossed my way.
> Please help. I dont want to learn C.

Ah, it's a pity.  C is my favorite compiled procedural language.

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data structure and algorithms

2007-01-29 Thread Beej
On Jan 29, 1:50 pm, "azrael" <[EMAIL PROTECTED]> wrote:
> thanks guys. i see that there is no way then to go back to C to
> satisfy my prof and get a grade

Seconding what Dennis says below, it is totally possible to use Python 
for this.

I didn't mean to discourage you from using Python--I just wanted to 
discourage you from trying to do it using the Python built-in list [].

In fact, given the time constraints and your dislike of structs, 
you're likely going to rip your hair out trying to get it going in C.

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data structure and algorithms

2007-01-29 Thread Beej
On Jan 29, 10:15 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> The instructor learned his lesson: no more assignments
> done in "any language I  can understand"

Without naming names, there was a person at my university who gained a 
certain amount of notoriety by implementing a file system for OS class 
in Bourne Shell.

That prof also changed the rule the very next semester. :-)

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help me understand this

2007-01-30 Thread Beej
On Jan 29, 11:47 pm, Steven D'Aprano 
<[EMAIL PROTECTED]> wrote:
> Outside of a print statement (and also an "except" statement), commas
> create tuples.

And function calls:

>>> 3,
(3,)
>>> type(3,)

>>> type((3,))


But here's one I still don't get:

>>> type(2)

>>> type((2))

>>> (2).__add__(1)
3
>>> 2.__add__(1)
  File "", line 1
2.__add__(1)
^
SyntaxError: invalid syntax

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help me understand this

2007-01-30 Thread Beej
On Jan 30, 1:38 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Because 2. is the start of a float-literal. That isn't distinguishable for
> the parsere otherwise.

Oh, excellent! I wonder why I didn't think of that--I was too busy in 
"get a field" mode it didn't even occur to me that the "." had a 
different context, no matter how much more obvious.

>>> print 2.
2.0
>>> type(2.)


-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help me understand this

2007-01-30 Thread Beej
On Jan 30, 9:52 am, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> A float is, too.  2.__add is a float followed by an identifier.
> Not legal.  As pointed out elsewhere in the thread, (2). forces
> it to be an integer followed by a ".".

Which leads to these two beauties:

>>> (2.).__add__(1)
3.0
>>> 2..__add__(1)
3.0

I like the second one more. :-)

-Beej


-- 
http://mail.python.org/mailman/listinfo/python-list


Getting file line numbers from Nodes in a DOM?

2007-02-08 Thread Beej
I have a DOM parsed with xml.dom.mindom.parse()...for a particular
Node, is there an easy way to get the line (and maybe even column)
numbers that the element appeared in the original file?

Thanks,
-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rot13 in a more Pythonic style?

2007-02-14 Thread Beej
On Feb 14, 9:04 am, "Andy Dingley" <[EMAIL PROTECTED]> wrote:
> I still don't understand what a lambda is _for_ in Python.

Python supports functional programming to a certain extent, and
lambdas are part of this.

http://linuxgazette.net/109/pramode.html

> I know what
> they are, I know what the alternatives are, but I still haven't found
> an instance where it permits something novel to be done that couldn't
> be done otherwise (if maybe not so neatly).

Strictly speaking, you can live your whole life without using them.
There's always a procedural way of doing things, as well.

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Python Call Graph 0.3.0

2007-02-14 Thread Beej
On Feb 14, 3:02 pm, "Gerald Kaszuba" <[EMAIL PROTECTED]> wrote:
> I just released pycallgraph 0.3.0. There are many examples on the web
> site and linked from the web site including a 16188 x 4187 sized call
> graph!

That's pretty cool, all right.

-Beej

-- 
http://mail.python.org/mailman/listinfo/python-list