Re: [Tutor] list to numpy record array

2010-02-23 Thread Skipper Seabold
On Mon, Feb 22, 2010 at 11:50 PM, Vincent Davis
 wrote:
>
> I must be missing something simple. I have a list of lists data = "[['  0', ' 
>  0', '234.0', '24.0', ' 25'], ['  1', '  0', '22428.0', '2378.1', ' 
> 25'],.." and what to make a record array from it but it gets screwed up 
> or I don't get it, maybe both. Notice that at this stage the items are 
> strings, not numbers, and there is whitespace not sure this matters.
> Here is what is happening
> adata = numpy.array(data,numpy.float64)
>
> >>> adata
> array([[  0.e+00,   0.e+00,   2.3400e+02,
>           2.4000e+01,   2.5000e+01],
>        ...,
>        [  4.7700e+02,   4.7700e+02,   2.0700e+02,
>           4.5800e+01,   2.5000e+01]])
>
> This is what I would expect except it is not a record array.
> This is not what I expect. I think I have tried every iteration including 
> using numpy dtaypes numpy.int32 or bdata = numpy.array(data, dtype = [('x', 
> int),('y', int),('mean',float),('stdv',float),('npixcels',int)])
> What am I missing?
>
> bdata = numpy.array(data, [('x', int),('y', 
> int),('mean',float),('stdv',float),('npixcels',int)])
> >>> bdata
> array([[(3153952, 0, 0.0, 0.0, 0), (3153952, 0, 0.0, 0.0, 0),
>         (206933603122, 0, 0.0, 0.0, 0), (808334386, 0, 0.0, 0.0, 0),
>         (3486240, 0, 0.0, 0.0, 0)],
>        [(3219488, 0, 0.0, 0.0, 0), (3153952, 0, 0.0, 0.0, 0),
>         (1356161439282, 0, 0.0, 0.0, 0),
>         (54074581398322, 0, 0.0, 0.0, 0), (3486240, 0, 0.0, 0.0, 0)],
>        [(3285024, 0, 0.0, 0.0, 0), (3153952, 0, 0.0, 0.0, 0),
>         (206933931058, 0, 0.0, 0.0, 0), (925775666, 0, 0.0, 0.0, 0),
>         (3486240, 0, 0.0, 0.0, 0)],
>        ...,
>        [(3487540, 0, 0.0, 0.0, 0), (3618612, 0, 0.0, 0.0, 0),
>         (206933602866, 0, 0.0, 0.0, 0), (908996661, 0, 0.0, 0.0, 0),
>         (3486240, 0, 0.0, 0.0, 0)],
>        [(3553076, 0, 0.0, 0.0, 0), (3618612, 0, 0.0, 0.0, 0),
>         (13561596370041137, 0, 0.0, 0.0, 0),
>         (62870573495603, 0, 0.0, 0.0, 0), (3486240, 0, 0.0, 0.0, 0)],
>        [(3618612, 0, 0.0, 0.0, 0), (3618612, 0, 0.0, 0.0, 0),
>         (206933798962, 0, 0.0, 0.0, 0), (942552372, 0, 0.0, 0.0, 0),
>         (3486240, 0, 0.0, 0.0, 0)]],
>
>       dtype=[('x', ' ('npixcels', '
>

I neglected to reply to the whole list on my first try.  For posterity's sake:

You should ask on the scipy-user list with a self-contained example.
It is heavily trafficked. http://www.scipy.org/Mailing_Lists

>From the example you gave above, I am not sure what's going unless
it's something in the casting from strings.  Note though that you have
created a structured array and not a record array.  The subtle
difference is that the record array allows attribute lookup ie., you
could do bdata.x instead of bdata['x'].  Structured arrays are usually
faster as the attribute lookup convenience is implemented in Python
whereas the structured arrays use C code.

hth,

Skipper
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OOP / Classes questions

2009-04-10 Thread Skipper Seabold
On Fri, Apr 10, 2009 at 2:44 PM, Stefan Lesicnik  wrote:
> I guess as a general kind of question, can anyone recommend some
> tutorial or documentation re OOP and classes?

I've found Alan Gauld's tutorials to be very useful when just getting
started in both OOP and Python.

http://www.freenetpages.co.uk/hp/alan.gauld/

hth,

Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a dict-like class - asigning variables... this one may take some thought ; )

2009-05-28 Thread Skipper Seabold
On Thu, May 28, 2009 at 6:47 AM, John [H2O]  wrote:
>
> Hello, I am trying to create a class to hold and reference things similar to
> matlab's structure.
>
> ## A class definition to hold things
> class stuff(object):
>    """ holds stuff """
>    def __init__():
>        pass
>   �...@classmethod
>    def items(cls):
>        stuff = []
>        for i in cls.__dict__:
>            if i[:1] != '_' and i != 'items':
>                stuff.append((i,cls.__dict__[i]))
>        return stuff
>
> Then in my code I would like to be able to do the follow:
>
> s = stuff
> s.cheese = "Brie"
> s.country = "French"
> s.age = 2
>
> and so on...
>
> In Ipython, as above it does work. Now here is the tricky part. I'm reading
> in binary data from unformatted Fortan output. My present approach is as
> follows (recommended suggestions welcome):
>
> f2=file(filename,'rb')
> #Define names and create Dictionary Keys
> I = {0:'rl0', 1:'ibdate', 2:'ibtime', 3:'version',\
>         4:'rl1', 5:'loutstep', 6:'loutaver', 7:'loutsample',\
>         8:'rl2', 9:'outlon0', 10:'outlat0', 11:'numxgrid',\
>         12:'numygrid', 13:'dxout', 14:'dyout', 15:'rl3', 16:'numzgrid',\
>         }
> #define the format for binary reading first part of the header file
> Dfmt=['i','i','i','13s','2i','i','i','i','2i','f','f','i','i','f','f','2i','i']
> if f2:
>    #print filename + ' has been opened'
>    a=[struct.unpack(ft,f2.read(struct.calcsize(ft))) for ft in Dfmt]
>
> # Now I want to put them into my stuff class:
>    for j in range(len(a)):
>        cmd = "h.%s = a[%s][0]" % (I[j],j)
>        eval(cmd)
>
>
> But I get an error:
>    eval(cmd)
>  File "", line 1
>    h.rl0 = a[0][0]
>          ^
> SyntaxError: invalid syntax
>
> Thoughts? Must be something simpler or more 'pythonic'
>
> Thanks!

For what it's worth, you might want to have a look at NumPy/SciPy
 unless you have some other motive.  Their
structured or record arrays behave in much the way you are describing
and can handled binary data (or matlab data).  This also might be of
interest .

Cheers,

Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating interactive program with python

2009-05-29 Thread Skipper Seabold
On Thu, May 28, 2009 at 7:43 PM, Andre Walker-Loud  wrote:
> Hi All,
>
> I am thinking of creating a data analysis suite with python, and I want it
> to be interactive - ie the program asks the user (me) for input, like which
> data file to use, how many parameters to minimize, etc.  There are a few
> features I want it to have, like graphing, both the initial data as well as
> results of my analysis, etc.  If you are familiar with it, Matlab is close
> to what I would like, a sort of all in one tool - but that is expensive
> commercial software.
>
> So I have a few questions.  And I should also say I would classify myself as
> a medium level python scripter, but I have not used the more advanced
> programming features, and I am a beginning programmer.  That being said, I
> am interested in expanding my python knowledge, especially getting into the
> more advanced features.  Even though this is ultimately just a tool to do my
> job (limiting my time to play around and learn new things) I generally find
> python so easy to use (and fun) that I am interested in this little project
> of mine (and also it will help me sever my dependency on Mathematica, if you
> are familiar with that program).
>
> Thanks
>
> Andre
>
>
>
> 1) I am trying to trying to figure out how I would set up an interactive
> python script, however, I am having trouble googling this, since a search
> for "interactive python" just returns instructions on how to type "python"
> in a terminal to get an "interactive" session
>
> I have in mind a while loop that asks the user for arguments, and then some
> switch making sure I have passed enough sys.args to proceed, but I am not
> sure how to prompt the user for arguments and count them locally (not a
> global number of sys.argv which is what I am used to now).  So any advice on
> where to look at an example of this would be great.
>
> 2) I have come across the "Scipy Superpack"
>
> http://macinscience.org/?page_id=6
>
> which contains Scipy, Numpy, Matplotlib, iPython and PyMC.  So it seems the
> installation would be convenient since it is all bundled together.  Anyone
> have experience with this package?
>
>
> 3) I see iPython (which I have never used) is an enhanced interactive python
> shell.  Perhaps this could do what I want - does anyone have experience with
> iPython?  Even if iPython can do everything I want (and more I am sure) I
> would still like to write my own basic little interactive script to expand
> my python knowledge.
>

You might want to have a look at embedding ipython.  Here is one take
(more at the link below):



Note that it plays well with matplotlib and has some *really*
convenient features for doing scientific computing/ data analysis with
Python.

 completion and being able to issue commands to the system shell
are worth the (free) price of admission alone for me, but that doesn't
even begin to scratch the surface of iPython's nice features.

Have a look at the documentation and the cookbook for tricks and embedding here:



>
> 4) does anyone have strong recommendations for or against using matplotlib?
>  Is it low maintenance for producing graphs, or does it take lots of user
> interaction?  All the example pictures I see are very pretty, but it is not
> clear how much effort is needed to generate them - or is it just a "well
> know to those who know it well" thing?
>

I have had plenty of luck with iPython, NumPy/SciPy, and matplotlib.

You can see some examples here:

For matplotlib:


For Data analysis with SciPy in general (graphical output is not
solely in matplotlib:


Cheers,

Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Discussion of super

2009-06-14 Thread Skipper Seabold
Hello all,

I am working under the umbrella of the Python Software Foundation for
the Google Summer of Code and am keeping a blog about the work.  Part
of my work is refactoring and extending some existing code.  This code
makes use of Python's super, and so I am trying to understand the ins
and outs of super.  I recently summarized my understanding of super on
my blog, and I was wondering if anyone would care to comment on my
write up.  I am particularly worried about my handling of my multiple
inheritance example and explanation of class F.

I apologize for the length and if this is an inappropriate request for
this list, but I read the discussions here almost daily, and often the
points that come up add to my Python understanding very much.
Everyone seems to have a great grasp of Python and programming and
that rare ability (and willingness) to *explain* topics.

The post is here


But if it's easier for the ML, comments can be inlined below.  The
In/Out prompts are the iPython interpreter if anyone is wondering, and
I tried to preserve indents.

Cheers,

Skipper

--

The current statistical models package is housed in the NiPy,
Neuroimaging in Python, project. Right now, it is designed to rely on
Python's built-in super to handle class inheritance. This post will
dig a little more into the super function and what it means for the
design of the project and future extensions. Note that there are
plenty of good places to learn about super and that this post is to
help me as much as anyone else. You can find the documentation for
super here. If this is a bit confusing, it will, I hope, become
clearer after I demonstrate the usage.

First, let's take a look at how super actually works for the simple
case of single inheritance (right now, we are not planning on using
multiple inheritance in the project) and an __init__ chain (note that
super can call any of its parent class's methods, but using __init__
is my current use case).

The following examples were adapted from some code provided by mentors
(thank you!).

class A(object):
def __init__(self, a):
self.a = a
print 'executing A().__init__'

class B(A):
def __init__(self, a):
self.ab = a*2
print 'executing B().__init__'
super(B,self).__init__(a)

class C(B):
def __init__(self, a):
self.ac = a*3
print 'executing C().__init__'
super(C,self).__init__(a)

Now let's have a look at creating an instance of C.

In [2]: cinst = C(10)
executing C().__init__
executing B().__init__
executing A().__init__

In [3]: vars(cinst)
Out[3]: {'a': 10, 'ab': 20, 'ac': 30}

That seems simple enough. Creating an instance of C with a = 10 will
also give C the attributes of B(10) and A(10). This means our one
instance of C has three attributes: cinst.ac, cinst.ab, cinst.a. The
latter two were created by its parent classes (or superclasses)
__init__ method. Note that A is also a new-style class. It subclasses
the 'object' type.

The actual calls to super pass the generic class 'C' and a handle to
that class 'self', which is 'cinst' in our case. Super returns the
literal parent of the class instance C since we passed it 'self'. It
should be noted that A and B were created when we initialized cinst
and are, therefore, 'bound' class objects (bound to cinst in memory
through the actual instance of class C) and not referring to the class
A and class B instructions defined at the interpreter (assuming you
are typing along at the interpreter).

Okay now let's define a few more classes to look briefly at multiple
inheritance.

class D(A):
def __init__(self, a):
self.ad = a*4
print 'executing D().__init__'
# if super is commented out then __init__ chain ends
#super(D,self).__init__(a)

class E(D):
def __init__(self, a):
self.ae = a*5
print 'executing E().__init__'
super(E,self).__init__(a)

Note that the call to super in D is commented out. This breaks the
__init__ chain.

In [4]: einst = E(10)
executing E().__init__
executing D().__init__

In [5]: vars(einst)
Out[5]: {'ad': 40, 'ae': 50}

If we uncomment the super in D, we get as we would expect

In [6]: einst = E(10)
executing E().__init__
executing D().__init__
executing A().__init__

In [7]: vars(einst)
Out[7]: {'a': 10, 'ad': 40, 'ae': 50}

Ok that's pretty straightforward. In this way super is used to pass
off something to its parent class. For instance, say we have a little
more realistic example and the instance of C takes some timeseries
data that exhibits serial correlation. Then we can have C correct for
the covariance structure of the data and "pass it up" to B where B can
then perform OLS on our data now that it meets the assumptions of OLS.
Further B can pass this data to A and return some descriptive
st

Re: [Tutor] newton's method for system of nonlinear equations

2009-06-26 Thread Skipper Seabold
On Fri, Jun 26, 2009 at 6:54 AM,  wrote:
> Hi,
> I am trying to write a program in python that solves a system of nonlinear
> equations using newton's method. I don't know what I am doing wrong.
> Please help
>
> from scipy import*
>
> x = array([0.0,0.0,0.0])
> n=len(x)
> tol= 0.1
> N=30
>
> k=1
> while k <= N:
>    def f(x):
>        f= zeros((len(x)),float)
>        f[0][k]= x[0][k]**2 + x[1][k]-37
>        f[1][k]=x[0][k]- x[1][k]**2- 5
>        f[2][k]= x[0][k] + x[1][k]+ x[2][k]- 3
>        return f[k]
>        def J(x):
>            J= zeros((n,n),float)
>            for i in range(n):
>                ei=zeros(n,float)
>                ei[i]=1.0
>                J[:i]=(f(x[k]+tol*ei)-f(x[k]))/tol
>                return J
>
>        y[k] = -(J.I)*f[k]
>        x[k+1]=x[k]+y[k]
>
> if sqrt(dot(f0,f0)/len(x)) < tol: print x
> else:
>    k=k+1
>
> print 'Too many iterations'
>
>

You might want to have a look at scipy.optimize


If you have more (somewhat) SciPy related questions, you might want to
ask them on the SciPy mailing list.  It's very active.


Cheers,

Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using datetime and calculating hourly average

2009-07-07 Thread Skipper Seabold
On Tue, Jul 7, 2009 at 6:16 AM, John [H2O] wrote:
>
> Here's a function I wrote to calculate hourly averages:
>
> It seems a bit slow, however... any thoughts on how to improve it?
>
> def calc_hravg(X):
>    """Calculates hourly average from input data"""
>
>    X_hr = []
>    minX = X[:,0].min()
>    hr = dt.datetime(*minX.timetuple()[0:4])
>
>    while hr <= dt.datetime(*X[-1,0].timetuple()[0:4]):
>        nhr = hr + dt.timedelta(hours=1)
>        ind = np.where( (X[:,0] > hr) & (X[:,0] < nhr) )
>        vals = X[ind,1][0].T
>        try:
>            #hr_avg = np.sum(vals) / len(vals)
>            hr_avg = np.average(vals)
>
>        except:
>            hr_avg = np.nan
>        X_hr.append([hr,hr_avg])
>        hr = hr + dt.timedelta(hours=1)
>
>    return np.array(X_hr)
>
>
> --

One quick thought, as I haven't read your code very carefully, but
using reduce is faster than sum or average (though you sacrifice
readability) if the ndarray is big enough to matter ie., instead of
np.average(vals) you could have np.add.reduce(vals)/len(vals).  You
might have some better luck on the numpy mailing list
.  It's very active and there are
people that are much more knowledgeable than me.  You might want to
include an example of your X in the example to help them help you to
optimize.

Cheers,
Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What kind of number is this

2009-07-24 Thread Skipper Seabold
2009/7/24 Emad Nawfal (عماد نوفل) :
> Hi Tutors,
> I have a bunch of text files that have many occurrences like the following
> which I believe, given the context,  are numbers:
>
> ١٨٧٢
>
> ٥٧
>
>  ٢٠٠٨
>
> etc.
>
> So, can somebody please explain what kind of numbers these are, and how I
> can get the original numbers back. The files are in Arabic and were
> downloaded from an Arabic website.
> I'm running python2.6 on Ubuntu 9.04
>
> --
> لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
> الغزالي
> "No victim has ever been more repressed and alienated than the truth"
>
> Emad Soliman Nawfal
> Indiana University, Bloomington
> 
>
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

Googling the first number, they appear to be the HTML encoding of
(literally) Arabic numerals (?)

١

http://en.wiktionary.org/wiki/%D9%A1

-Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] curve fitting

2009-07-29 Thread Skipper Seabold
On Wed, Jul 29, 2009 at 9:42 AM, Eike Welk wrote:
> On Wednesday 29 July 2009, Bala subramanian wrote:
>> Friends,
>>
>> I wish to do some curve fitting with python by defining my own
>> equations. Could someone please give some guidance or examples on
>> doing the same.
>

What kind of curve fitting exactly?  Linear equations?  Can you
provide an example?

> You can use the Numpy/Scipy libraries for that. I think they have
> examples for curve fitting on their website. But unfortunately the
> website is down (or my Internet is broken).
>
> http://www.scipy.org/
>

There were some problems with the site yesterday as well.  Scipy would
be a good place to start.  I am in the home stretch of completing a
google summer of code project to integrate some mostly linear
statistical models (different flavors of least squares fitting,
generalized linear models, robust statistics, generalized additive
models, etc.) into SciPy, which might also be of interest to you
depending on your needs (some info here
).  There are also some "cookbook"
examples from the scipy page that show some recipes for doing some
basic curve fitting (OLS, interpolation) with the available tools.

> There are also special mailing lists for Numpy/Scipy/Matplotlib users,
> to which you should subscribe. This one would be good for your
> question:
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
> If I understand you right, you have a formula with some parameters.
> Now you are searching for parameter values so that the formula really
> goes through the data points. This is a task for optimization
> functions.
>
> Kind regards,
> Eike.
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] curve fitting

2009-07-29 Thread Skipper Seabold
On Wed, Jul 29, 2009 at 10:27 AM, Bala
subramanian wrote:
> I have to do the following:
>
> Eq 1) F = A * FB + (1-A) FO - (1)
>
> Eq 2) A = 1/2a0 [ ( ao + x + bo) - { ( ao + x + bo)2 - 4 aobo ) }0.5 ]
> . (2)
>
> KNOWN: F, FB, FO, ao,
> UNKNOWN:  bo
>
> I have to fit the data first to Eq 1, find A and then fit it to Eq 2 to find
> bo. I know python programming but am very new to this kind of analysis. I
> would greatly appreciate if you could provide me some guidance on how to do
> the same.
>
> Thanks,
> Bala
>

It's good practice to reply-all to a mailing list and not to top post
(replying at the top of an email), so it's easier to read and follow
the discussion.

What is your fitting criterion?  Ie., are you trying to minimize the
sum of squared errors (least squares), etc.  There are different
fitting criteria depending on what kind of data you have and where
your noise is expected to come from.

It looks like you could do this with a least squares fit if you want,
putting linear constraints on the coefficients (which should be a part
of the scipy.models soon), or you could rearrange the equations to get
your unknown in only one place.

Eq 1 would then be F = FO + A * (FB - FA)

You can do similarly for equation 2 I think but didn't look closely.
I'm still not sure what everything is in those equations.  If you ask
over on the scipy-user list and include the equations and a data
example, you will almost certainly get some more help.

Cheers,

Skipper

>
> On Wed, Jul 29, 2009 at 3:59 PM, Skipper Seabold 
> wrote:
>>
>> On Wed, Jul 29, 2009 at 9:42 AM, Eike Welk wrote:
>> > On Wednesday 29 July 2009, Bala subramanian wrote:
>> >> Friends,
>> >>
>> >> I wish to do some curve fitting with python by defining my own
>> >> equations. Could someone please give some guidance or examples on
>> >> doing the same.
>> >
>>
>> What kind of curve fitting exactly?  Linear equations?  Can you
>> provide an example?
>>
>> > You can use the Numpy/Scipy libraries for that. I think they have
>> > examples for curve fitting on their website. But unfortunately the
>> > website is down (or my Internet is broken).
>> >
>> > http://www.scipy.org/
>> >
>>
>> There were some problems with the site yesterday as well.  Scipy would
>> be a good place to start.  I am in the home stretch of completing a
>> google summer of code project to integrate some mostly linear
>> statistical models (different flavors of least squares fitting,
>> generalized linear models, robust statistics, generalized additive
>> models, etc.) into SciPy, which might also be of interest to you
>> depending on your needs (some info here
>> <http://scipystats.blogspot.com/>).  There are also some "cookbook"
>> examples from the scipy page that show some recipes for doing some
>> basic curve fitting (OLS, interpolation) with the available tools.
>>
>> > There are also special mailing lists for Numpy/Scipy/Matplotlib users,
>> > to which you should subscribe. This one would be good for your
>> > question:
>> > http://projects.scipy.org/mailman/listinfo/scipy-user
>> >
>> >
>> > If I understand you right, you have a formula with some parameters.
>> > Now you are searching for parameter values so that the formula really
>> > goes through the data points. This is a task for optimization
>> > functions.
>> >
>> > Kind regards,
>> > Eike.
>> > ___
>> > Tutor maillist  -  tu...@python.org
>> > http://mail.python.org/mailman/listinfo/tutor
>> >
>> ___
>> Tutor maillist  -  tu...@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dealing with bitfields in Python

2009-08-30 Thread Skipper Seabold
Hello all,

Fair warning, I didn't know what a bitfield was a few hours ago.

I am working with a program via the dbus module and I am wondering if
there is built-in support to deal with bitfields in Python.  I query
my application and it returns a bitfield 119.  The bitfield "key" is

NONE  = 0,
CAN_GO_NEXT   = 1 << 0,
CAN_GO_PREV   = 1 << 1,
CAN_PAUSE = 1 << 2,
CAN_PLAY  = 1 << 3,
CAN_SEEK  = 1 << 4,
CAN_PROVIDE_METADATA  = 1 << 5,
CAN_HAS_TRACKLIST = 1 << 6

And a call to the method returns 119.  I have gotten far enough to
understand that 119 is

>>> (1<<0)+(1<<1)+(1<<2)+(0<<3)+(1<<4)+(1<<5)+(1<<6)
119

119 is 01110111 as a binary byte (I'm reaching back to high school
computer science here...)

So I guess I understand the basics of what it's telling me, but I'd
like to unpack 119 into binary, so I can read it and use the
information in my program.  I've adapted a code snippet that I found
online to do this, but I'm wondering if there is a better way in
python maybe using binascii or struct?

Here is the helper function I've adapated

def int_2_binary(int):
const = 0x8000
output = ""
## for each bit
for i in range(1,33):
## if the bit is set, print 1
if( int & const ):
output = output + "1"
else:
output = output + "0"
## shift the constant using right shift
const = const >> 1
output = list(output)
output = "".join(output[-8:])
return output

As you can see const is the smallest signed 32-bit integer, and it
would return a length 32 string.  But I know that my bitfield will be
8-bit, I just don't know what this is in hexadecimal (?) to define
const.  Any pointers to do this in a better way would be appreciated.

Thanks,

Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dealing with bitfields in Python

2009-08-30 Thread Skipper Seabold
On Sun, Aug 30, 2009 at 1:25 PM, Luke
Paireepinart wrote:
> You can just do a binary AND with your consts and your bitfield value to get
> each bit.  The values will be the actual value (2, 4, 8, etc.) but if you
> use these as a boolean it won't matter.
> I.E.
>>>> bitfield = 119
>>>> seek = bitfield & CAN_SEEK
>>>> seek
> 16
>>>> if seek:
> print "Hello"
> Hello
>>>>
> if you have to have these constants defined.
> The way I'd actually do it is probably without defining constants.
> #unpack bitfield values
> go_next, go_prev, pause, play, seek, meta, tracklist = [(1< for i in range(7)]
> If you need them as booleans for some reason,
> go_next, go_prev, pause, play, seek, meta, tracklist = [((1<
> 0) for i in range(7)]
> If this isn't what you're asking, just clarify what you meant and I'll try
> to help.
> HTH,
> -Luke

No that's perfect.  It looks like I need to read up a bit more (pun
not intended) on the binary AND, but I think I see what it does and I
understand your example.

Thanks,

Skipper

> On Sun, Aug 30, 2009 at 5:59 PM, Skipper Seabold 
> wrote:
>>
>> Hello all,
>>
>> Fair warning, I didn't know what a bitfield was a few hours ago.
>>
>> I am working with a program via the dbus module and I am wondering if
>> there is built-in support to deal with bitfields in Python.  I query
>> my application and it returns a bitfield 119.  The bitfield "key" is
>>
>> NONE                  = 0,
>> CAN_GO_NEXT           = 1 << 0,
>> CAN_GO_PREV           = 1 << 1,
>> CAN_PAUSE             = 1 << 2,
>> CAN_PLAY              = 1 << 3,
>> CAN_SEEK              = 1 << 4,
>> CAN_PROVIDE_METADATA  = 1 << 5,
>> CAN_HAS_TRACKLIST     = 1 << 6
>>
>> And a call to the method returns 119.  I have gotten far enough to
>> understand that 119 is
>>
>> >>> (1<<0)+(1<<1)+(1<<2)+(0<<3)+(1<<4)+(1<<5)+(1<<6)
>> 119
>>
>> 119 is 01110111 as a binary byte (I'm reaching back to high school
>> computer science here...)
>>
>> So I guess I understand the basics of what it's telling me, but I'd
>> like to unpack 119 into binary, so I can read it and use the
>> information in my program.  I've adapted a code snippet that I found
>> online to do this, but I'm wondering if there is a better way in
>> python maybe using binascii or struct?
>>
>> Here is the helper function I've adapated
>>
>> def int_2_binary(int):
>>    const = 0x8000
>>    output = ""
>>    ## for each bit
>>    for i in range(1,33):
>>        ## if the bit is set, print 1
>>        if( int & const ):
>>            output = output + "1"
>>        else:
>>            output = output + "0"
>>        ## shift the constant using right shift
>>        const = const >> 1
>>    output = list(output)
>>    output = "".join(output[-8:])
>>    return output
>>
>> As you can see const is the smallest signed 32-bit integer, and it
>> would return a length 32 string.  But I know that my bitfield will be
>> 8-bit, I just don't know what this is in hexadecimal (?) to define
>> const.  Any pointers to do this in a better way would be appreciated.
>>
>> Thanks,
>>
>> Skipper
>> ___
>> Tutor maillist  -  tu...@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dealing with bitfields in Python

2009-08-30 Thread Skipper Seabold
On Sun, Aug 30, 2009 at 1:58 PM, Alan Gauld wrote:
>
> "Skipper Seabold"  wrote
>
>> You can just do a binary AND with your consts and your bitfield value to
>> get
>> > each bit. The values will be the actual value (2, 4, 8, etc.) but if >
>> > you
>> > use these as a boolean it won't matter.
>> No that's perfect.  It looks like I need to read up a bit more (pun
>> not intended) on the binary AND, but I think I see what it does and I
>> understand your example.
>
> You can go to the Using the OS topic in my tutorial for a quick tutoerial
> on how to extracts bits from a bitfield. There is a "sidebar" box entitled
> "Bitwise Operations and Flags" which explains the idea
>

Thanks.  Your description is (of course) as clear as day.

Cheers,

Skipper
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding to n significant digits

2009-09-01 Thread Skipper Seabold
On Tue, Sep 1, 2009 at 9:44 PM, Richard Wagner wrote:
> I'm fairly new to Python and am trying to find a simple way to round floats
> to a specific number of significant digits.  I found an old post on this
> list with exactly the same problem:
>

Python 2.5.4 (r254:67916, Apr  4 2009, 17:56:17)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print round(2.55, 2)
2.56

http://docs.python.org/library/functions.html#round

Skipper
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding to n significant digits

2009-09-01 Thread Skipper Seabold
On Tue, Sep 1, 2009 at 10:00 PM, Skipper Seabold wrote:
> On Tue, Sep 1, 2009 at 9:44 PM, Richard Wagner wrote:
>> I'm fairly new to Python and am trying to find a simple way to round floats
>> to a specific number of significant digits.  I found an old post on this
>> list with exactly the same problem:
>>
>
> Python 2.5.4 (r254:67916, Apr  4 2009, 17:56:17)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> print round(2.55, 2)
> 2.56
>
> http://docs.python.org/library/functions.html#round
>
> Skipper
>

Sorry didn't read carefully enough.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] i can't for the life of me get "#! /usr/bin/env python" or "#!/usr/bin/python" to work

2009-10-21 Thread Skipper Seabold
On Wed, Oct 21, 2009 at 5:56 PM, Jason Willis  wrote:
> so i changed the .bashrc and added at the end :
> PATH="/home/compy/pythons:$PATH"  ###which is the actual path to my python
> proggies###
>
> and i still get
> co...@compy-laptop:~/pythons$ herosinventory.py
> herosinventory.py: command not found
> co...@compy-laptop:~/pythons$ herosinventory.py
> herosinventory.py: command not found
> co...@compy-laptop:~/pythons$ herosinventory
> herosinventory: command not found
> co...@compy-laptop:~/pythons$
>
>
> I must be retarded. I'm sorry , what am I doing wrong here?
>

You might need to restart X (or just the terminal, can't remember) or
type "source ~/.bashrc" (no quotes) at the command line, if you added
the correct line to your bash profile.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor