Well, for FreeBSD and OpenBSD, at least. I can't yet vouch for NetBSD and Dragonfly BSD.
First, FreeBSD - the port is named py-freebsd. Once built, the module can be imported with "import freebsd".
[carl@pcbsd]/usr/local/lib/python2.6/site-packages(158)% python
Python 2.6.2 (r262:71600, Jun 24 2009, 23:31:28)
[GCC 4.2.1 20070719 [FreeBSD]] on freebsd7
Type "help", "copyright", "credits" or "license" for more information.
>>> import freebsd
>>> dir(freebsd)
['__doc__', '__file__', '__name__', '__package__', '__version__', 'chflags', 'const', 'fchflags', 'fstatfs', 'geom_getxml', 'getfsent', 'getfsfile', 'getfsspec', 'getfsstat', 'gethostname', 'getloadavg', 'getlogin', 'getosreldate', 'getpriority', 'getprogname', 'getpwent', 'getpwnam', 'getpwuid', 'getquota', 'getrlimit', 'getrusage', 'ifstats', 'ipstats', 'jail', 'kevent', 'kqueue', 'ktrace', 'lchflags', 'quotaoff', 'quotaon', 'quotasync', 'reboot', 'sendfile', 'sethostname', 'setlogin', 'setpriority', 'setproctitle', 'setprogname', 'setquota', 'setrlimit', 'statfs', 'sysctl', 'sysctldescr', 'sysctlmibtoname', 'sysctlnametomib', 'tcpstats', 'udpstats']
Not a bad collection of utilities. Let's take a couple for a test drive:
>>> freebsd.gethostname()
'pcbsd'
>>> freebsd.getprogname()
'python'
>>> help(freebsd.jail)
Help on built-in function jail in module freebsd:
jail(...)
jail(path, hostname, ip_number):
The jail() system call sets up a jail and locks the current process
in it. The ``path'' should be set to the directory which is to be
the root of the prison. The ``hostname'' can be set to the hostname
of the prison. This can be changed from the inside of the prison.
The ``ip_number'' can be set to the IP number assigned to the prison.
>>> # wow, you can set up a jail with python
>>> freebsd.ifstats()
>>> >>> import pprint
>>> pprint.pprint(_)
{'bge0': {'addrlen': 6,
'baudrate': 100000000L,
'collisions': 0L,
'flags': 34883,
'hdrlen': 14,
'hwassist': 7L,
'ibytes': 19222590L,
'ierrors': 0L,
'imcasts': 577L,
'ipackets': 19728L,
'iqdrops': 0L,
'metric': 0L,
'mtu': 1500L,
'name': 'bge0',
'noproto': 0L,
'obytes': 2009038L,
'oerrors': 0L,
'omcasts': 0L,
'opackets': 13285L,
'pcount': 0,
'physical': 0,
'snd_drops': 0,
'snd_len': 0,
'snd_maxlen': 511,
'type': 6},
bge0 is the ethernet device on my Thinkpad.
>>> freebsd.getlogin()
'carl'
>>> freebsd.tcpstats()
>>> pprint.pprint(_)
{'accepts': 0L,
'badsyn': 0L,
'cachedrtt': 147L,
'cachedrttvar': 150L,
'cachedssthresh': 4L,
'closed': 495L,
'connattempt': 360L,
'conndrops': 20L,
'connects': 340L,
'delack': 277L,
'drops': 22L,
'keepdrops': 0L,
'keepprobe': 0L,
'keeptimeo': 0L,
'listendrop': 0L,
'mturesent': 0L,
'pawsdrop': 0L,
'persistdrop': 0L,
'persisttimeo': 0L,
'predack': 0L,
'preddat': 15226L,
'rcvackbyte': 1093284L,
'rcvackpack': 1848L,
'rcvacktoomuch': 0L,
'rcvafterclose': 7L,
'rcvbadoff': 0L,
'rcvbadsum': 0L,
'rcvbyte': 16595286L,
'rcvbyteafterwin': 0L,
'rcvdupack': 232L,
'rcvdupbyte': 88723L,
'rcvduppack': 77L,
'rcvoobyte': 1015050L,
'rcvoopack': 919L,
'rcvpack': 15882L,
'rcvpackafterwin': 0L,
'rcvpartdupbyte': 525L,
'rcvpartduppack': 2L,
'rcvshort': 0L,
'rcvtotal': 18489L,
'rcvwinprobe': 0L,
'rcvwinupd': 3L,
'rexmttimeo': 118L,
'rttupdated': 1817L,
'sc_aborted': 0L,
'sc_added': 0L,
'sc_badack': 0L,
'sc_bucketoverflow': 0L,
'sc_cacheoverflow': 0L,
'sc_completed': 0L,
'sc_dropped': 0L,
'sc_dupsyn': 0L,
'sc_recvcookie': 0L,
'sc_reset': 0L,
'sc_retransmitted': 0L,
'sc_sendcookie': 0L,
'sc_stale': 0L,
'sc_unreach': 0L,
'sc_zonefail': 0L,
'segstimed': 1688L,
'sndacks': 9261L,
'sndbyte': 1098259L,
'sndctrl': 697L,
'sndpack': 1252L,
'sndprobe': 0L,
'sndrexmitbyte': 2252L,
'sndrexmitpack': 2L,
'sndtotal': 12381L,
'sndurg': 0L,
'sndwinup': 1169L,
'timeoutdrop': 9L}
22 drops, 9 of them timeouts, and a bunch of other stuff too.
Enough for today. Next time we'll take a quick look at the Python module for OpenBSD.
Sunday, January 31, 2010
Thursday, January 28, 2010
Python Logos III: Pycon India 2009 contest
Just received an e-mail from Noufal this morning (actually it was a comment on one of the previous two logo posts) about the Indian Pycon logo contest last year. I've already shown the winner in a previous post, but the entries are worth seeing in their own right. Hats off to the contributors:
** SPECIAL NOTE: Some of the following logos do not comply with the Python Software Foundation's trademark rules for the use of the logo. Although there was no harm intended through use in such manner, it's always good to keep the full logo intact (don't split the snakes or take one away!). My very basic, and perhaps inaccurate understanding is that it's OK to use the logo and even change it's colors, but the shape should remain intact.
** SPECIAL NOTE: Some of the following logos do not comply with the Python Software Foundation's trademark rules for the use of the logo. Although there was no harm intended through use in such manner, it's always good to keep the full logo intact (don't split the snakes or take one away!). My very basic, and perhaps inaccurate understanding is that it's OK to use the logo and even change it's colors, but the shape should remain intact.
Friday, January 22, 2010
Monday, January 11, 2010
Python 3.1 String Formatting - Positional Arguments
Last time I did a goofy little trick with the new Python 3 string formatting. This time I'd like to demo something a little more practical.
I had heard a lot about positional arguments being more flexible and easier in Python 3's string formatting. The basic gist is that the positions being referred to are on the right side of the string's format method as opposed to the left. This usually results in fewer positions, and, hopefully, less confusion.
Quickie example:
>>> class Foo:
>>> def __init__(self):
>>> pass
>>>
>>> a = Foo()
>>> a.foo = 24
>>> a.bar = 33
>>> a.baz = 6
>>> # Python 2 string formatting
>>> # print '%d %d %d' % (a.foo, a.bar, a.baz)
>>> print('{0.foo} {0.bar} {0.baz}'.format(a))
24 33 6
It's true that the left side is a bit more involved now with the new string formatting. There are more options for formatting, which I have omitted. My main goal here is to understand the new formatting to the point where I can leverage it to my advantage in my work. Let's see what, if anything, I've learned.
In the mining industry I did a lot data tracking on various production metrics - flows through pipes, broken rock moved to its final location, explosive power, etc. One of the most challenging things to track was production in the open pit. It all centered around big multimillion dollar electric shovels.
Anyway, to make a long story longer, by the time you worked your way up the object hierarchy to the shovel, you ended up with about 20 or more "fields" in the database, or members of your shovel object. Smithing csv output from these objects could get pretty hairy, because there were so many fields to track, and they changed frequently.
Out of reality - it's just too complex - back to the programming world. This Python 3 string formatting could have helped out some in this situation. Let's go back to the Foo object:
UPDATE!
I had made an error in the print statment below by mixing quotes. It is now corrected.
Yet another example of why code must be tested :-\
>>> # make two more Foo objects
>>> b = Foo()
>>> b.foo = 'hello'
>>> b.bar = 'cruel'
>>> b.baz = 'world'
>>> c = Foo()
>>> c.foo = '99.234'
>>> c.bar = '23.715'
>>> c.baz = '18.6'
>>> for objx in a, b, c:
>>> for strx in 'foo', 'bar', 'baz':
>>> print(('{0} = {1.' + strx +
'}').format(
strx, objx))
foo = 24
bar = 33
baz = 6
foo = hello
bar = cruel
baz = world
foo = 99.234
bar = 23.715
baz = 18.6
Simple example, but it illustrates some of the utility of the new string formatting. If I were trying to track a bunch of data, I would only have to redefine my "fields" once in a global constant ['foo', 'bar', 'baz']. objx could be cycling (actually, iterating) through a list of objects. Any writing or crunching could be done in a separate (small) function or method outside the loop (since ['foo', 'bar', 'baz'] is constant).
Turns out this new string formatting is something I could get used to . . . ;-)
I had heard a lot about positional arguments being more flexible and easier in Python 3's string formatting. The basic gist is that the positions being referred to are on the right side of the string's format method as opposed to the left. This usually results in fewer positions, and, hopefully, less confusion.
Quickie example:
>>> class Foo:
>>> def __init__(self):
>>> pass
>>>
>>> a = Foo()
>>> a.foo = 24
>>> a.bar = 33
>>> a.baz = 6
>>> # Python 2 string formatting
>>> # print '%d %d %d' % (a.foo, a.bar, a.baz)
>>> print('{0.foo} {0.bar} {0.baz}'.format(a))
24 33 6
It's true that the left side is a bit more involved now with the new string formatting. There are more options for formatting, which I have omitted. My main goal here is to understand the new formatting to the point where I can leverage it to my advantage in my work. Let's see what, if anything, I've learned.
In the mining industry I did a lot data tracking on various production metrics - flows through pipes, broken rock moved to its final location, explosive power, etc. One of the most challenging things to track was production in the open pit. It all centered around big multimillion dollar electric shovels.
Anyway, to make a long story longer, by the time you worked your way up the object hierarchy to the shovel, you ended up with about 20 or more "fields" in the database, or members of your shovel object. Smithing csv output from these objects could get pretty hairy, because there were so many fields to track, and they changed frequently.
Out of reality - it's just too complex - back to the programming world. This Python 3 string formatting could have helped out some in this situation. Let's go back to the Foo object:
UPDATE!
I had made an error in the print statment below by mixing quotes. It is now corrected.
Yet another example of why code must be tested :-\
>>> # make two more Foo objects
>>> b = Foo()
>>> b.foo = 'hello'
>>> b.bar = 'cruel'
>>> b.baz = 'world'
>>> c = Foo()
>>> c.foo = '99.234'
>>> c.bar = '23.715'
>>> c.baz = '18.6'
>>> for objx in a, b, c:
>>> for strx in 'foo', 'bar', 'baz':
>>> print(('{0} = {1.' + strx +
'}').format(
strx, objx))
foo = 24
bar = 33
baz = 6
foo = hello
bar = cruel
baz = world
foo = 99.234
bar = 23.715
baz = 18.6
Simple example, but it illustrates some of the utility of the new string formatting. If I were trying to track a bunch of data, I would only have to redefine my "fields" once in a global constant ['foo', 'bar', 'baz']. objx could be cycling (actually, iterating) through a list of objects. Any writing or crunching could be done in a separate (small) function or method outside the loop (since ['foo', 'bar', 'baz'] is constant).
Turns out this new string formatting is something I could get used to . . . ;-)
Thursday, January 7, 2010
Fun With Python 3.1's New String Formatting . . .
Or a totally unoriginal poor ASCII art rendition of the Transamerica building at night . . .
carl@L733rLinux:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> # saw this on Python-Dev (Stephen Bethard?)
>>> format('HEADER', '*^50')
'**********************HEADER**********************'
# just for fun . . .
>>> for x in range(30):
... print('{0:*^50}'.format(x * ' '))
...
**************************************************
************************ *************************
************************ ************************
*********************** ************************
*********************** ***********************
********************** ***********************
********************** **********************
********************* **********************
********************* *********************
******************** *********************
******************** ********************
******************* ********************
******************* *******************
****************** *******************
****************** ******************
***************** ******************
***************** *****************
**************** *****************
**************** ****************
*************** ****************
*************** ***************
************** ***************
************** **************
************* **************
************* *************
************ *************
************ ************
*********** ************
*********** ***********
********** ***********
>>>
If that isn't beautiful, I don't know what is . . .
carl@L733rLinux:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> # saw this on Python-Dev (Stephen Bethard?)
>>> format('HEADER', '*^50')
'**********************HEADER**********************'
# just for fun . . .
>>> for x in range(30):
... print('{0:*^50}'.format(x * ' '))
...
**************************************************
************************ *************************
************************ ************************
*********************** ************************
*********************** ***********************
********************** ***********************
********************** **********************
********************* **********************
********************* *********************
******************** *********************
******************** ********************
******************* ********************
******************* *******************
****************** *******************
****************** ******************
***************** ******************
***************** *****************
**************** *****************
**************** ****************
*************** ****************
*************** ***************
************** ***************
************** **************
************* **************
************* *************
************ *************
************ ************
*********** ************
*********** ***********
********** ***********
>>>
If that isn't beautiful, I don't know what is . . .
Subscribe to:
Posts (Atom)






































