Re: Dynamically reference member of array

2014-03-29 Thread R. Michael Weylandt
On Wed, Mar 26, 2014 at 7:43 AM, Ben Collier bmcoll...@gmail.com wrote:
 Hi all,

 I know that I can dynamically reference a variable with locals()[i], for 
 instance, but I'd like to know how to do this with a variable in an object.

 If I have an object called device, with variables called attr1, attr2 .. 
 attr50, how could I dynamically reference these?


I think the pythonic term you're looking for is attribute. See
getattr() and setattr()

Cheers,
Michael
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Algorithm that makes maximum compression of completly diffused data.

2013-11-07 Thread R. Michael Weylandt michael.weyla...@gmail.com


On Nov 7, 2013, at 21:25, jonas.thornv...@gmail.com wrote:

 Den fredagen den 8:e november 2013 kl. 03:17:36 UTC+1 skrev Chris Angelico:
 On Fri, Nov 8, 2013 at 1:05 PM,  jonas.thornv...@gmail.com wrote:
 
 I guess what matter is how fast an algorithm can encode and decode a big 
 number, at least if you want to use it for very big sets of random data, or 
 losless video compression?
 
 
 
 I don't care how fast. I care about the laws of physics :) You can't
 
 stuff more data into less space without losing some of it.
 
 
 
 Also, please lose Google Groups, or check out what other people have
 
 said about making it less obnoxious.
 
 
 
 ChrisA
 
 Please, you are he obnoxious, so fuck off or go learn about reformulation of 
 problems. Every number has an infinite number of arithmetical solutions. So 
 every number do has a shortest arithmetical encoding. And that is not the 
 hard part to figure out, the hard part is to find a generic arithmetic 
 encoding.
 
 I am not sure if it is just stupidness or laziness that prevent you from 
 seeing that 4^8=65536.

Chris's point is more subtle: the typical computer will store the number 65536 
in a single byte, but it will also store 4 and 8 in one byte. So if your choice 
is between sending 65536 and (4,8), you actually loose efficiency in your 
scheme. Don't think in decimal, but in terms of information needing transfer. 

You might reply that you don't need a whole byte for 4 or 8 -- that's true. You 
could, e.g., just encode the fourth and eight bits of a single byte and send 
that: certainly gives some compression but at the cost of generality-- what 
would you do with 65^65? It's sort of like the mean-variance tradeoff in 
statistics; it's much easier to encode certain data sets (e.g. powers of two as 
you noted) but only if you concede your algorithm will only work for those 
values. 

More generally, check out the work of Claud Shannon; a very accessible and 
worthwhile author. 
 -- 
 https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Algorithm that makes maximum compression of completly diffused data.

2013-11-07 Thread R. Michael Weylandt michael.weyla...@gmail.com


On Nov 7, 2013, at 22:24, Chris Angelico ros...@gmail.com wrote:

 On Fri, Nov 8, 2013 at 1:43 PM, R. Michael Weylandt
 michael.weyla...@gmail.com michael.weyla...@gmail.com wrote:
 Chris's point is more subtle: the typical computer will store the number 
 65536 in a single byte, but it will also store 4 and 8 in one byte. 
 
 Well, 65536 won't fit in a single byte, nor even in two (only just). A
 typical binary representation of 65536 would take 3 bytes, or 4 for a
 common integer type: 0x00 0x01 0x00 0x00 (big-endian). 

Quite right -- meant C's int type, (machine word) not char. My mistake!

Michael
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A certainl part of an if() structure never gets executed.

2013-06-16 Thread R. Michael Weylandt
On Sun, Jun 16, 2013 at 10:59 AM, Nick the Gr33k supp...@superhost.gr wrote:
 On 16/6/2013 12:22 μμ, Denis McMahon wrote:

 For example, in Python

 a = 6
 b = a
 c = 6

 a and b point to one memory location that contains the value 6
 c points to a different memory location that contains the value 6


 I believe you are mistaken.

 a here is not a pointer but variable,
 which is a memory location that stores value 6.

 b here is a pointer. It's value is the memory location of variable a which
 stores value 6.

 c here is just te same as a , a variable.

Actually, y'all both might be. This is a bit CPython specific and not
mandated by the language specification.

To Nikos: please don't extrapolate from the examples below. They are a
CPython (the most common implementation of the Python language)
specific detail.

## CODE SNIPPET##
a = 6; b = a; c = 6

id(a)
id(b)
id(c)
## END CODE##

These are all the same, indicating that they all point to the same 6
in memory. That's a CPython specific optimization (caching small
integers) which is not guaranteed by the language and changes between
pythons and between compiles.

For example,

## CODE SNIPPET##
a = 552315251254
b = a
c =  552315251254

a is b # True _on my machine_
a is c # False _on my machine_

id(a)
id(b)
id(c)
## END CODE##

Note that to compare if two names point to the same object, you can
use the is operator.

a is b
c is a

etc.


 A pointer = a variable that has as a value a memory address a variable =
 a memory address that has as a value the actual value we want to store.


 These are really C terms, not Python terms. Stop thinking that C is
 behaving like Python.



 I think it behaves the same way, but lets here from someone else too.

I understand the Greeks invented democracy and all that, but facts
aren't subject to it.


 Whats the difference of interpreting  to compiling ?

If only it could be googled Alas, no one has ever written anything
about technology on the internet. Ironic that...

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


Re: A certainl part of an if() structure never gets executed.

2013-06-16 Thread R. Michael Weylandt
On Sun, Jun 16, 2013 at 12:06 PM, Ferrous Cranus supp...@superhost.gr wrote:

I appreciate you've returned to your Ferrous Cranus persona for this
interchange. It reminds me not to get hung up on concerns of
futility...

 On 16/6/2013 1:42 μμ, R. Michael Weylandt wrote:


 ## CODE SNIPPET##
 a = 552315251254
 b = a
 c =  552315251254

 a is b # True _on my machine_


 I accept this if in the premise that, b boils down to actually point to a's
 value, but it has to go through a first to find it, not directly.

You don't get to accept it on a premise or not. It's simply a matter of fact.

In fact, b does not go through a. The memory address referenced
exists even if the a binding is removed using del a or some other
mechanism. Imagine this scenario:

[a]
\
 6
   /
[b]

Using the name a or b simply tells Python where to look for a
value, but the value itself is not associated with a or b and will
only be removed if both a and b are del'd.

If we remove a while keeping b alive, we still have a means of
getting to 6 so Python's GC / RefCounter won't remove it. This
implies that the memory can't be solely owned by a.

[a] # Binding gone now or perhaps referring to something else

 6
/
[b]

And this pattern continues for any sort of Python object.


 a is c # False _on my machine_


 Why false?  These are 2 different memory locations storing the same number.
 This should have been True.

Again, you have the idea that you can use words like should here. It
either is or isn't. There's simply no normative claim involved.


 what id() does, never heard of that function before.


It seems you've also never heard of Python's help function?

Try

help(id)

at your interactive prompt and see what happens.


 --
 What is now proved was at first only imagined!

Depth of stubbornness?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A certainl part of an if() structure never gets executed.

2013-06-16 Thread R. Michael Weylandt
On Sun, Jun 16, 2013 at 2:38 PM, Ferrous Cranus supp...@superhost.gr wrote:
 On 16/6/2013 3:04 μμ, R. Michael Weylandt wrote:
 ## CODE SNIPPET##
 a = 552315251254
 b = a
 c =  552315251254

 a is b # True _on my machine_


 And this pattern continues for any sort of Python object.
 a is c # False _on my machine_

 And in mine is also True.


What do you mean also true here? You can't be also true in the
presence of a false. This makes little sense to me...


 id(a)
 140160465571760
 id(b)
 140160465571760
 id(c)
 140160465571696

 Since all object result to point to actual number 6 why don't all of them
 (a,b,c) bound to the same memory address.

Look at the code they follow. (At least in this email) -- none of them
point to an object with value 6. They all point to a much larger
value.


 a and b seem both objects of the same identity, which means they are both
 bound to the same memory address(140160465571760)

Yes


 how come c's memory address is different than a's and b's ?
 After all, this is the 3rd object pointing to number 6.

Again, no it's not.


 And why not d and e and f and g are all objects of the same memory address?

What are these variables? They are referenced nowhere in any mail
between you and me.



a and b are the same object, with two different names. (No if I want
about it -- the distinction is important)

No idea what you mean by unbounded memory address here. (Yes I'm
aware I deleted a fairly meaningless line about it from the context)


 No i had no idea thagt was a bult-in help() function.
 So id() is actually the memory address of an object which uniquely
 identifies it.

Yes.


I think part of your confusion is coming from the dual interpretation
of the = operator in an expression like:

LHS = RHS

If RHS is some sort of literal, a conforming Python behaves as if
(modulo optimizations described below) a new object is created
according to that literal, put in memory and the name LHS is used to
point to it.

If RHS is a name (as in code such as a = b), the Python will find
the object to which b refers and add a new reference (a) to it.

Some information on the CPython specific implementation details:

As part of the startup process, CPython creates some number of small
integers and adds them to the object cache.

Why? Because creating an object (even as simple as an int) is
relatively expensive and small integers are ubiquitous in programming.
This sacrifices some memory for a good deal of speed. But the payoff
becomes less as you go higher: almost all programs will use a 0 or a
2, fewer will use 247. At some point, the pre-creation stops. I
believe this is a compile time option.

Now, to make effective use of this caching, CPython will use a few
tricks to try to identify when one of the small ints appears in the
code. If it can identify it, I will replace the object creation with a
reference to the in-cache object. The exact times when this can happen
are not -- to my knowledge -- documented anywhere.

Now you might ask why Python doesn't realize that, in code like my
code snippet above, it could simply reuse the same int object for a,
b, and c. Conceivably it could, but it would require enormous energies
to check all currently existing objects for one that happens to be
equal to what you need (and is not mutable if that's relevant) and
it's simply faster to create the new int.

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


Re: A certainl part of an if() structure never gets executed.

2013-06-16 Thread R. Michael Weylandt
On Sun, Jun 16, 2013 at 2:47 PM, Ferrous Cranus supp...@superhost.gr wrote:
 On 16/6/2013 2:13 μμ, Jussi Piitulainen wrote:

 If, instead of the above, you have

 a = 6
 b = a
 b = 5

 you will find that b == 5 and a == 6. So b is not the same as a. Else
 one would have changed when the other changed. I would say that a and
 b are different variables. They had the same value, briefly.


 If they were different variables then they would have different memory
 addresses and they would act like two different objects.

 But... both a and b are for a fact mappings for the same memory address as
 seen form the following command.

 id(a) == id(b)
 True

 They are like the same object with 2 different names.

This will depend on when the test is run:

a = 6
b = a
a is b # True

b = 5
a is b # False

The latter is false because the binding of b to the int 6 was broken
in order to bind b to the int 5.

I might also suggest you restrain from trying to correct respondents
on these matters until you yourself understand them. It's only polite.

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


Re: A certainl part of an if() structure never gets executed.

2013-06-14 Thread R. Michael Weylandt
On Fri, Jun 14, 2013 at 9:03 AM, Nick the Gr33k supp...@superhost.gr wrote:
 name=abcd
 month=efgh
 year=ijkl

 print(name or month or year)
 abcd

 Can understand that, it takes the first string out of the 3 strings that has
 a truthy value.

 print(k in (name and month and year))
 True

 No clue. since the expression in parenthesis returns 'abcd' how can 'k'
 contained within 'abcd' ?

No it's not. See both above (where you use 'or' instead) and below
where _you yourself_ show that it's not 'abcd.'

Now read: https://en.wikipedia.org/wiki/Short-circuit_evaluation
noting especially the specified behavior for Python. If you find it
too technical, google for other uses of the terms.


 print(name and month and year)
 ijkl

 Seems here is returning the last string out of 3 strings, but have no clue
 why Python doing this.

Think about basic logic: 'or' means 'is at least one true?' so Python
only has to look at the first 'truthy value'. 'and' means 'are they
all true?' so Python has to look at all the values, ending up with the
last one, unless a 'falsey value' is found before.

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


Re: A certainl part of an if() structure never gets executed.

2013-06-14 Thread R. Michael Weylandt
On Fri, Jun 14, 2013 at 9:24 AM, R. Michael Weylandt
michael.weyla...@gmail.com wrote:
 On Fri, Jun 14, 2013 at 9:03 AM, Nick the Gr33k supp...@superhost.gr wrote:

 No clue. since the expression in parenthesis returns 'abcd' how can 'k'
 contained within 'abcd' ?

 No it's not. See both above (where you use 'or' instead) and below
 where _you yourself_ show that it's not 'abcd.'

s/it's not/it doesn't return/g

Typos always and forever,
MW
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish = Greek (subprocess complain)

2013-06-07 Thread R. Michael Weylandt
On Fri, Jun 7, 2013 at 9:10 AM, Νικόλαος Κούρας nikos.gr...@gmail.com wrote:
 On 7/6/2013 10:42 πμ, Michael Weylandt wrote:

 os.rename( filepath_bytes filepath.encode('utf-8')

 Missing comma, which is, after all, just a matter of syntax so it can't
 matter, right?

 I doubted that os.rename arguments must be comma seperated.

All function calls in Python require commas if you are putting in more
than one argument. [0]

 But ater reading the docs.

 s.rename(src, dst)

 Rename the file or directory src to dst. If dst is a directory, OSError will
 be raised. On Unix, if dst exists and is a file, it will be replaced
 silently if the user has permission. The operation may fail on some Unix
 flavors if src and dst are on different filesystems. If successful, the
 renaming will be an atomic operation (this is a POSIX requirement). On
 Windows, if dst already exists, OSError will be raised even if it is a file;
 there may be no way to implement an atomic rename when dst names an existing
 file.

 Availability: Unix, Windows.

 Indeed it has to be:

 os.rename( filepath_bytes, filepath.encode('utf-8')

Parenthesis missing here as well.


 'mv source target' didn't require commas so i though it was safe to assume
 that os.rename did not either.


That's for shell programming -- different language entirely.

The surrogate business is back to Unicode, which ain't my specialty so
I'll leave that to more able programmers.

MW

[0] You could pass multiple arguments by way of a tuple or dictionary
using */** but if you want arguments that aren't in the container
being passed, you're back to needing commas.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using pandoc instead of rst to document python

2013-04-24 Thread R. Michael Weylandt
On Wed, Apr 24, 2013 at 12:14 AM, Peng Yu pengyu...@gmail.com wrote:
 I currently use sphinx to generate the doc (in rst). How to figure it
 to support pandoc's markdown?

If I understand the desired workflow, it's just 1) write in markdown;
2) then run pandoc to convert to rst; 3) then run Sphinx to render
html or whatever you want.

You could even script this in python ;-)

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


Re: using pandoc instead of rst to document python

2013-04-23 Thread R. Michael Weylandt
On Tue, Apr 23, 2013 at 6:36 PM, Peng Yu pengyu...@gmail.com wrote:
 Hi,

 I'm wondering if it possible to use pandoc instead of rst to document
 python. Is there a documentation system support this format of python
 document?

Pandoc is a converter while rst is a format so they're not directly
comparable; pandoc can convert _to_ and _from_ rst to a wide variety
of other formats, but you still have to write documentation in one
format or another. If you want to use an rst-centric documentation
tool, you can write in, e.g., Markdown, convert to rst and then run
your other tool on it.

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


Re: monty python

2013-03-20 Thread R. Michael Weylandt
It's lexigraphic (order by first letter, but if those are the same,
compare the second, but if those are same compare the third, ... if
one ends while the other continues, it's considered 'lower') on the
character's ASCII (binary encoding values):

http://www.asciitable.com/

Note that all the upper case values appear before the lower case
values. (And there are some other 'characters' like newline before
that but you won't see them)

Cheers,
Michael

On Wed, Mar 20, 2013 at 1:33 PM, franzferdinand
melo.dumou...@hotmail.com wrote:
 Monty  Python
 True
 Z  a
 True
 Monty  Montague

 False
 What's the rule about that? Is it the number of letters or what?
 thanks
 --
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Division matrix

2012-11-13 Thread R. Michael Weylandt
On Tue, Nov 13, 2012 at 1:00 AM, Cleuson Alves cleuso...@gmail.com wrote:
 Hello, I need to solve an exercise follows, first calculate the inverse 
 matrix and then multiply the first matrix.

I would just point out that in most numerical applications, you rarely
need to calculate the intermediate of the matrix inverse directly.
See, e.g., http://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/

Of course, if this hasn't been said yet: NumPy.

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


Re: Invalid syntax

2012-11-07 Thread R. Michael Weylandt
On Wed, Nov 7, 2012 at 11:17 PM, Smaran Harihar
smaran.hari...@gmail.com wrote:
 Hi guys,

 I am stuck in one of those non identifiable error location in the code. The
 code keeps giving invalid syntax. This is my code.

 I am using the same code for another code and not sure why this is not
 working. This is the traceback that I am getting.

 Any idea where am I going wrong?

If the dpaste link is correct, you're missing a close-parenthesis at
the end of the w.record line:

w.record(collection[i][0], MAT[0], TSD[0], AnnTMin[0], ANNPREC[0],
float(collection[i][2]), float(collection[i][1]) ## --- Hereabouts

Cheers,
Michael
-- 
http://mail.python.org/mailman/listinfo/python-list