How to simulate C style integer division?

2016-01-21 Thread Shiyao Ma
Hi,

I wanna simulate C style integer division in Python3.

So far what I've got is:
# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
  result = math.floor(result)
else:
  result = math.ceil(result)


I found it's too laborious. Any quick way?

-- 

吾輩は猫である。ホームーページはhttps://introo.me 。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Recurring Prompt

2016-01-21 Thread Johannes Findeisen
On Wed, 20 Jan 2016 11:30:19 -0800
Mansi wrote:

> Hello,
> 
> I just installed Python 3.5.1 but anytime I use Pycharm, a prompt of whether 
> I want to modify, repair or uninstall Python keeps on coming up. Even while 
> I'm in the midst of typing a program. Please advise. 

I believe, You should ask the Pycharm People about this.

Python is not Pycharm... :|

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


Re: How to simulate C style integer division?

2016-01-21 Thread Yann Kaiser
You can use the // operator, which should do what you want.

On Thu, Jan 21, 2016, 09:40 Shiyao Ma  wrote:

> Hi,
>
> I wanna simulate C style integer division in Python3.
>
> So far what I've got is:
> # a, b = 3, 4
>
> import math
> result = float(a) / b
> if result > 0:
>   result = math.floor(result)
> else:
>   result = math.ceil(result)
>
>
> I found it's too laborious. Any quick way?
>
> --
>
> 吾輩は猫である。ホームーページはhttps://introo.me 。
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Peter Heitzer
Shiyao Ma  wrote:
>Hi,

>I wanna simulate C style integer division in Python3.

>So far what I've got is:
># a, b = 3, 4

>import math
>result = float(a) / b
>if result > 0:
>  result = math.floor(result)
>else:
>  result = math.ceil(result)


>I found it's too laborious. Any quick way?
In Python3 you have to use the floor division operator '//'
For example:
result=a//b



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


Re: Same function but different names with different set of default arguments

2016-01-21 Thread Yann Kaiser
Apparently the thread poster cannot receive email, I just received a bounce
on my previous email :-/

On Thu, Jan 21, 2016, 08:49 Yann Kaiser  wrote:

> partial treats keyword arguments as default values, though they become
> keyword-only as a result :
>
> f1 = functools.partial(g, p="p1")
>
> On Thu, Jan 21, 2016, 08:35 Paulo da Silva <
> [email protected]> wrote:
>
>> Hi all.
>>
>> What is the fastest implementation of the following code?
>>
>> def g(p):
>> ...
>> return something
>>
>> def f1(p="p1"):
>> return g(p)
>>
>> def f2(p="p2"):
>> return g(p)
>>
>> Thanks
>> Paulo
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Jussi Piitulainen
Shiyao Ma writes:

> I wanna simulate C style integer division in Python3.
>
> So far what I've got is:
> # a, b = 3, 4
>
> import math
> result = float(a) / b
> if result > 0:
>   result = math.floor(result)
> else:
>   result = math.ceil(result)
>
> I found it's too laborious. Any quick way?

The general principle is to define a function when something is too
laborious to do inline. def truncdiv(a, b): ...

But math.trunc rounds towards 0. Maybe you want to use that here.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Ben Finney
Shiyao Ma  writes:

> I wanna simulate C style integer division in Python3.

I'm not sure I know exactly what behaviour you want (“C style” may mean
different things to each of us).

I'll point out that Python's ‘//’ operator specifies floor division
https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations>.

-- 
 \   “Timid men prefer the calm of despotism to the boisterous sea |
  `\of liberty.” —Thomas Jefferson |
_o__)  |
Ben Finney

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


Re: Same function but different names with different set of default arguments

2016-01-21 Thread Peter Otten
Paulo da Silva wrote:

> Hi all.
> 
> What is the fastest implementation of the following code?
> 
> def g(p):
> ...
> return something
> 
> def f1(p="p1"):
> return g(p)
> 
> def f2(p="p2"):
> return g(p)

>>> def g(p): return p.upper()
... 
>>> def f1(): pass
... 
>>> f1.__code__ = g.__code__
>>> f1.__defaults__ = ("p1",)
>>> f1()
'P1'

Not recommended ;)

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


Re: How to simulate C style integer division?

2016-01-21 Thread Paul Rubin
Ben Finney  writes:
> I'm not sure I know exactly what behaviour you want (“C style” may mean
> different things to each of us).

I thought he meant trunc-division, so -5 / 2 = -2 and -5 % 2 = -1.
Python specifies floor division but C leaves it unspecified, I thought.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Jussi Piitulainen
Paul Rubin writes:

> Ben Finney writes:
>> I'm not sure I know exactly what behaviour you want (“C style” may mean
>> different things to each of us).
>
> I thought he meant trunc-division, so -5 / 2 = -2 and -5 % 2 = -1.
> Python specifies floor division but C leaves it unspecified, I thought.

I found a statement that it's specified to be truncating since C99.
Before that it was flooring or truncating.

Reading OP's code, the intention seemed clear to me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Jussi Piitulainen
Jussi Piitulainen writes:
> Shiyao Ma writes:
>
>> I wanna simulate C style integer division in Python3.
>>
>> So far what I've got is:
>> # a, b = 3, 4
>>
>> import math
>> result = float(a) / b
>> if result > 0:
>>   result = math.floor(result)
>> else:
>>   result = math.ceil(result)
>>
>> I found it's too laborious. Any quick way?
>
> The general principle is to define a function when something is too
> laborious to do inline. def truncdiv(a, b): ...
>
> But math.trunc rounds towards 0. Maybe you want to use that here.

It's actually best to avoid floating point altogether. The following
answer by Abhijit was linked from the StackOverflow page[1] where I
found out about C99 integer division:

def trunc_div(a,b):
q, r = divmod(a,b)
if  q < 0 and r:
q += 1
return q

It adjusts a negative floored quotient towards zero if there was a
remainder.

[1] 
http://stackoverflow.com/questions/15633787/truncated-versus-floored-division-in-python?rq=1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Oscar Benjamin
On 21 January 2016 at 08:39, Shiyao Ma  wrote:
>
> I wanna simulate C style integer division in Python3.
>
> So far what I've got is:
> # a, b = 3, 4
>
> import math
> result = float(a) / b
> if result > 0:
>   result = math.floor(result)
> else:
>   result = math.ceil(result)
>
>
> I found it's too laborious. Any quick way?

How precise do you want to be? I think that having a negative divisor
doesn't come up much in practise but this matches in all cases anyway:

   atruncb = abs(a) // abs(b) if a * b > 0 else - (abs(a) // abs(b))

If you don't care about the negative divisor case then it becomes:

   atruncb = abs(a) // b if a > 0 else - (abs(a) // b)

Note that for integers in Python these are exact and will work for
integers of any size. Using floating point is unnecessary here and
would have overflow problems.

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


Installing pygame

2016-01-21 Thread John Mycroft

Hi!
I have now spent several hours trying to install Pygame with Python 
3.5.  I have installed from a msi file "successfully" but "import 
pygame" fails either because Python can't find pygame or because "%1  is 
not a valid .DLL".  I have followed the instructions at 
https://www.webucator.com/blog/2015/03/installing-the-windows-64-bit-version-of-pygame/ 
to install from a wheel which works just fine until I get to

***
C:\Python>c:\python\scripts\pip install pygame-1.9.2a0-cp35-none-win32
c:\python\lib\site-packages\pip\pep425tags.py:89: RuntimeWarning: Config 
variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect

  warn=(impl == 'cp')):
c:\python\lib\site-packages\pip\pep425tags.py:93: RuntimeWarning: Config 
variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect

  warn=(impl == 'cp')):
Collecting pygame-1.9.2a0-cp35-none-win32
  Could not find a version that satisfies the requirement 
pygame-1.9.2a0-cp35-none-win32 (from versions: )

No matching distribution found for pygame-1.9.2a0-cp35-none-win32
**
which tells me nothing.


Please, someone - how do I install pygame?  It appears to be installed 
on my PC - maybe I have it in the wrong folder?  When I download the 
install packages, I copy them into my c:\Python folder (where my Python 
lives) and install from there so I would think they'd get installed in 
the right place.


Many thanks - John Mycroft
--
https://mail.python.org/mailman/listinfo/python-list


Re: Installing pygame

2016-01-21 Thread jacob Kruger
I literally just installed pyGame under 3.5.1, using following .whl file 
that pulled off a site offering collections of .whl files:

http://www.lfd.uci.edu/~gohlke/pythonlibs/

And, according to following page, the command of pi3p install 
...followed by name of .whl file... handled installing pyGame under 3.5.1:

https://skellykiernan.wordpress.com/2015/01/04/python-pygame-install/

That was after copying the .whl file into the./scripts directory under 
python 3.5.1 installation path.


HTH

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."

On 2016-01-21 12:10 PM, John Mycroft wrote:

Hi!
I have now spent several hours trying to install Pygame with Python 
3.5.  I have installed from a msi file "successfully" but "import 
pygame" fails either because Python can't find pygame or because "%1  
is not a valid .DLL".  I have followed the instructions at 
https://www.webucator.com/blog/2015/03/installing-the-windows-64-bit-version-of-pygame/ 
to install from a wheel which works just fine until I get to

***
C:\Python>c:\python\scripts\pip install pygame-1.9.2a0-cp35-none-win32
c:\python\lib\site-packages\pip\pep425tags.py:89: RuntimeWarning: 
Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect

  warn=(impl == 'cp')):
c:\python\lib\site-packages\pip\pep425tags.py:93: RuntimeWarning: 
Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect

  warn=(impl == 'cp')):
Collecting pygame-1.9.2a0-cp35-none-win32
  Could not find a version that satisfies the requirement 
pygame-1.9.2a0-cp35-none-win32 (from versions: )

No matching distribution found for pygame-1.9.2a0-cp35-none-win32
**
which tells me nothing.


Please, someone - how do I install pygame?  It appears to be installed 
on my PC - maybe I have it in the wrong folder?  When I download the 
install packages, I copy them into my c:\Python folder (where my 
Python lives) and install from there so I would think they'd get 
installed in the right place.


Many thanks - John Mycroft


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


Re: Same function but different names with different set of default arguments

2016-01-21 Thread Steven D'Aprano
On Thu, 21 Jan 2016 06:30 pm, Paulo da Silva wrote:

> Hi all.
> 
> What is the fastest implementation of the following code?

Let's find out. Here are three different ways to do it:

def g(p):
return

def f1(p=3):  # argument with a default
return g(p)

def f2():  # no argument at all
return g(3)

from functools import partial
f3 = partial(g, 3)


# setup timing code
from timeit import Timer
t1 = Timer("f1()", "from __main__ import f1")
t2 = Timer("f2()", "from __main__ import f2")
t3 = Timer("f3()", "from __main__ import f3")


Now let's see how long they take. This is using Python 2.7 on my computer.


py> min(t1.repeat(repeat=7))
0.43099188804626465
py> min(t1.repeat(repeat=7))
0.4344518184661865
py> min(t1.repeat(repeat=7))
0.42992687225341797



py> min(t2.repeat(repeat=7))
0.4341525878906
py> min(t2.repeat(repeat=7))
0.432689905166626
py> min(t2.repeat(repeat=7))
0.43417787551879883


py> min(t3.repeat(repeat=7))
0.281879186630249
py> min(t3.repeat(repeat=7))
0.27957892417907715
py> min(t3.repeat(repeat=7))
0.28043699264526367


There's no visible difference between the first and second method. The third
method, using functools.partial, is considerably faster, BUT remember that
this only effects the time it takes to call the function g(). If g()
actually does any work, the time spent doing the work will *far* outweigh
the overhead of calling the function.




-- 
Steven

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


Re: How to simulate C style integer division?

2016-01-21 Thread Steven D'Aprano
On Thu, 21 Jan 2016 08:11 pm, Ben Finney wrote:

> Shiyao Ma  writes:
> 
>> I wanna simulate C style integer division in Python3.
> 
> I'm not sure I know exactly what behaviour you want (“C style” may mean
> different things to each of us).

Surely is means "whatever the C standard defines integer division to mean".

Are their any C programmers here who can tell us what the C standard says?

According to this:

http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c

the behaviour of integer division was implementation dependent in C89, but
has now been specified in detail since C99. The answer is that a/b for
integers a and b *truncate towards zero*. That is, it returns the integer
part with no fractional part:

Both arguments are positive, or both negative:

11/2 = 5.5 so throw away the 0.5 and return 5

-11/-2 = 5.5 so throw away the 0.5 and return 5

One argument is positive and the other negative:

-11/2 = -5.5 so throw away the 0.5 and return -5

11/-2 = -5.5 so throw away the 0.5 and return -5


Python's integer division // performs flooring, not truncating, so it
disagrees in the case that exactly one of the arguments are negative:

py> 11//2
5
py> -11//-2
5

py> 11//-2
-6
py> -11//2
-6


So my guess is that the fastest, and certainly the most obvious, way to get
the same integer division behaviour as C99 would be:

def intdiv(a, b):
# C99 style integer division with truncation towards zero.
n = a//b
if (a < 0) != (b < 0):
n += 1
return n



-- 
Steven

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


Re: How to simulate C style integer division?

2016-01-21 Thread Jussi Piitulainen
Steven D'Aprano writes:

> So my guess is that the fastest, and certainly the most obvious, way
> to get the same integer division behaviour as C99 would be:
>
> def intdiv(a, b):
> # C99 style integer division with truncation towards zero.
> n = a//b
> if (a < 0) != (b < 0):
> n += 1
> return n

You should only increment if there is a (non-zero) remainder.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Wolfgang Maier

On 1/21/2016 15:00, Jussi Piitulainen wrote:

Steven D'Aprano writes:


So my guess is that the fastest, and certainly the most obvious, way
to get the same integer division behaviour as C99 would be:

def intdiv(a, b):
 # C99 style integer division with truncation towards zero.
 n = a//b
 if (a < 0) != (b < 0):
 n += 1
 return n


You should only increment if there is a (non-zero) remainder.



Right. Merging Steven's suggestion and fractions.Fraction.__trunc__ 
implementation I think the right answer may be:


def intdiv2(a,b):
# C99 style integer division with truncation towards zero.
if (a < 0) != (b < 0):
return -(-a // b)
else:
return a // b

Cheers,
Wolfgang


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


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


importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
What does "from (module) import (func)" do?

Please don't tell me that I shouldn't ask because real programmers
know not to have circular dependencies ...

I have no idea what was imported before.  I just want to import hexdump().
Unfortunately, this does not work:

  from utilities import hexdump

ImportError: cannot import name hexdump

Now, I shouldn't have a problem because the programmers before me didn't
understand either, and so just duplicated it everywhere, but I'd really
like to do it right.

I would have thought that "from" would allow me to import just one
symbol.  Now, for all the people just chomping at the bit to tell
me about circular dependencies, would you explain to me why I get this:

  (PDB)hexdump(msg)
*** NameError: name 'hexdump' is not defined


P.S. can I get a list of all the loaded symbols and/or modules?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Marko Rauhamaa
Jussi Piitulainen :

> Steven D'Aprano writes:
>
>> So my guess is that the fastest, and certainly the most obvious, way
>> to get the same integer division behaviour as C99 would be:
>>
>> def intdiv(a, b):
>> # C99 style integer division with truncation towards zero.
>> n = a//b
>> if (a < 0) != (b < 0):
>> n += 1
>> return n
>
> You should only increment if there is a (non-zero) remainder.

Maybe:

   def intdiv(a, b):
   return a // b if (a < 0) == (b < 0) else -(-a // b)


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


Re: Installing on linux - missing devel packages

2016-01-21 Thread Grant Edwards
On 2016-01-21, Chris Angelico  wrote:
> On Thu, Jan 21, 2016 at 6:18 PM, Frank Millman  wrote:
>> Fedora 22 comes standard with Python 3.4.2. I want to install 3.5.1.
>>
>> It is easy enough to download the source and run ./configure;make;make
>> altinstall. But then I find that I cannot import gzip because zlib-devel is
>> missing. I fix that, then I find that sqlite-devel is missing.
>>
>> Is there an easy way to find out all the missing components, so that when
>> the installation is complete I can be sure I have the entire standard lib?
>
> This is a Linux packaging question, more than a Python one. On Debian
> systems, the way to do that is "apt-get build-dep python3"; check your
> own package manager for an equivalent - it'll probably be called
> builddep or similar.

Similarly on Gentoo, one does "emerge python:3.5" to build whatever
the "current" python 3.5 version is (including any dependancies) or
"emerge =python-3.5.1-r2" if you want a specific version.

One would hope the Fedora would be able to do something similar.

-- 
Grant Edwards   grant.b.edwardsYow! The FALAFEL SANDWICH
  at   lands on my HEAD and I
  gmail.combecome a VEGETARIAN ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installing on linux - missing devel packages

2016-01-21 Thread Wolfgang Maier

On 1/21/2016 8:27, Chris Angelico wrote:


This is a Linux packaging question, more than a Python one. On Debian
systems, the way to do that is "apt-get build-dep python3"; check your
own package manager for an equivalent - it'll probably be called
builddep or similar.



Yes, you'd run:

dnf builddep python3

The list of packages this returns (30 to install freshly on Fedora23) is 
impressive though so I'm not sure you really need all of these.



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


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


Re: How to simulate C style integer division?

2016-01-21 Thread Jussi Piitulainen
Marko Rauhamaa writes:

> Jussi Piitulainen writes:
>
>> Steven D'Aprano writes:
>>
>>> So my guess is that the fastest, and certainly the most obvious, way
>>> to get the same integer division behaviour as C99 would be:
>>>
>>> def intdiv(a, b):
>>> # C99 style integer division with truncation towards zero.
>>> n = a//b
>>> if (a < 0) != (b < 0):
>>> n += 1
>>> return n
>>
>> You should only increment if there is a (non-zero) remainder.
>
> Maybe:
>
>def intdiv(a, b):
>return a // b if (a < 0) == (b < 0) else -(-a // b)

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


Re: importing: what does "from" do?

2016-01-21 Thread Ian Kelly
On Jan 21, 2016 7:31 AM, "Charles T. Smith" 
wrote:
>
> What does "from (module) import (func)" do?

Approximately equivalent to:

import module
func = module.func

Except that it doesn't bind "module" to anything.

> Please don't tell me that I shouldn't ask because real programmers
> know not to have circular dependencies ...
>
> I have no idea what was imported before.  I just want to import hexdump().
> Unfortunately, this does not work:
>
>   from utilities import hexdump
>
> ImportError: cannot import name hexdump

What is utilities? Is it a module on your sys.path?

> Now, I shouldn't have a problem because the programmers before me didn't
> understand either, and so just duplicated it everywhere, but I'd really
> like to do it right.
>
> I would have thought that "from" would allow me to import just one
> symbol.  Now, for all the people just chomping at the bit to tell
> me about circular dependencies

What makes you think this is caused by circular dependencies? It could be,
but you really haven't given us enough information about what utilities is
to diagnose that.

> would you explain to me why I get this:
>
>   (PDB)hexdump(msg)
> *** NameError: name 'hexdump' is not defined

Probably because the name 'hexdump' is not defined.

> P.S. can I get a list of all the loaded symbols and/or modules?

What do you mean by "loaded"? Bound to a name in the main module? In the
globals of the current scope? In any module? Try the "dir" or "vars"
built-in.

You can get a view of all the *cached* modules by looking in the
sys.modules dict.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
On Thu, 21 Jan 2016 07:52:17 -0700, Ian Kelly wrote:

>> I have no idea what was imported before.  I just want to import
>> hexdump(). Unfortunately, this does not work:
>>
>>   from utilities import hexdump
>>
>> ImportError: cannot import name hexdump
> 
> What is utilities? Is it a module on your sys.path?


Yes.


> What makes you think this is caused by circular dependencies? It could
> be, but you really haven't given us enough information about what
> utilities is to diagnose that.


Well, I thought that that's what causes these import problems.  I just
wish that python gave me more information, like the tree of imports that's
causing the problem.



>> would you explain to me why I get this:
>>
>>   (PDB)hexdump(msg)
>> *** NameError: name 'hexdump' is not defined
> 
> Probably because the name 'hexdump' is not defined.


If indeed it's not defined, then I wouldn't think there'd be a
problem importing the module that defines it.


>> P.S. can I get a list of all the loaded symbols and/or modules?
> 
> What do you mean by "loaded"? Bound to a name in the main module? In the
> globals of the current scope? In any module? Try the "dir" or "vars"
> built-in.
> 
> You can get a view of all the *cached* modules by looking in the
> sys.modules dict.


as in this?

  (PDB) pp dict (sys.modules)

Okay, quite good.  I have this (among others):


 'hexdump': ,
 'int.hexdump': None,


But I suspect the first, at any rate, was loaded because of this in
my ~/.pdbrc:

  from hexdump import hexdump

which wouldn't interfere when running without the debugger (that module
in my sys.path but nowhere near my project)


I have this in my project:

  int/__init__.py

But:

  $ ls int/hexdump*
  ls: cannot access int/hexdump*: No such file or directory

So I don't really understand "int.hexdump".
I have these function definitions:

  codec/objects.py:47:def hexdump (s, l = None):
  int/struct_phy.py:26:def hexdump (s, l = None):

both in modules.

It fails to load:

  from struct_phy import hexdump

or:

from int.struct_phy import hexdump
ImportError: cannot import name hexdump



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


Re: importing: what does "from" do?

2016-01-21 Thread John Gordon
In  "Charles T. Smith" 
 writes:

>   from utilities import hexdump

> ImportError: cannot import name hexdump

The most likely explanation here is that the 'utilities' module simply
does not contain something named 'hexdump'.

Have you inspected the 'utilities' module?  Does it, in fact, contain
something named 'hexdump'?

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
On Thu, 21 Jan 2016 15:31:45 +, John Gordon wrote:


> The most likely explanation here is that the 'utilities' module simply
> does not contain something named 'hexdump'.
> 
> Have you inspected the 'utilities' module?  Does it, in fact, contain
> something named 'hexdump'?

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


Re: importing: what does "from" do?

2016-01-21 Thread John Gordon
In  "Charles T. Smith" 
 writes:

> > The most likely explanation here is that the 'utilities' module simply
> > does not contain something named 'hexdump'.
> > 
> > Have you inspected the 'utilities' module?  Does it, in fact, contain
> > something named 'hexdump'?

> Yes

In that case, the problem is most likely a circular import issue, as you
mentioned.  The only way to fix it is to reorganize your modules.

How did this error come up?  Did the code work previously?  If so, what
changed?

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
On Thu, 21 Jan 2016 15:44:43 +, John Gordon wrote:


> How did this error come up?  Did the code work previously?  If so, what
> changed?

The developers of this legacy code had this as well as other functions
duplicated throughout the system, in order to avoid confronting these
issues.  I'm trying to untangle the mess - without rewriting thousands
of lines of application code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installing on linux - missing devel packages

2016-01-21 Thread Zachary Ware
On Thu, Jan 21, 2016 at 1:18 AM, Frank Millman  wrote:
> Hi all
>
> Fedora 22 comes standard with Python 3.4.2. I want to install 3.5.1.
>
> It is easy enough to download the source and run ./configure;make;make
> altinstall. But then I find that I cannot import gzip because zlib-devel is
> missing. I fix that, then I find that sqlite-devel is missing.
>
> Is there an easy way to find out all the missing components, so that when
> the installation is complete I can be sure I have the entire standard lib?

Other answers have given you the proper way to quickly get all the
build dependencies.  In order to see what modules you won't have
available, look at the output near the end of the 'make' command (it's
very quickly buried by `make altinstall`, so be sure to do those as
separate commands :)).  Most of the extension modules are built by
setup.py, which gives a report at the end of its run about which
modules it couldn't find dependencies for, which modules couldn't be
built, and which modules built but couldn't be imported.

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


Re: Single format descriptor for list

2016-01-21 Thread Paul Appleby
Thanks all for the answers.


Oscar Benjamin  wrote:

 print(('{}th\n' * len(a)).format(*a))
 print(''.join(map('{}th\n'.format, a)))

Those two look closest to what I was hoping for, I guess, but as Chris 
Angelico said, it probably is clearer to just loop over the range.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
On Thu, 21 Jan 2016 15:44:43 +, John Gordon wrote:

> In that case, the problem is most likely a circular import issue, as you
> mentioned.  The only way to fix it is to reorganize your modules.
> 
> How did this error come up?  Did the code work previously?  If so, what
> changed?


What I need is tools to find the problems.  In particular, why doesn't
python give me more of a clue where the problem is?  It would be cool
if it would tell me exactly what module to look at, which symbol(s)
were colliding.

The suggestion of using
pp dict (sys.modules)
might be useful, I need to study it in more detail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: importing: what does "from" do?

2016-01-21 Thread John Gordon
In  "Charles T. Smith" 
 writes:

> > How did this error come up?  Did the code work previously?  If so, what
> > changed?

> The developers of this legacy code had this as well as other functions
> duplicated throughout the system, in order to avoid confronting these
> issues.

Yes, but did the code work previously?  If so, what changed?

> I'm trying to untangle the mess - without rewriting thousands of lines
> of application code.

At worst you'd have to move some things into a new module, and change the
statements that import those things.  You shouldn't have to rewrite any of
the actual code.

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: importing: what does "from" do?

2016-01-21 Thread Ian Kelly
On Thu, Jan 21, 2016 at 8:12 AM, Charles T. Smith
 wrote:
>>> would you explain to me why I get this:
>>>
>>>   (PDB)hexdump(msg)
>>> *** NameError: name 'hexdump' is not defined
>>
>> Probably because the name 'hexdump' is not defined.
>
>
> If indeed it's not defined, then I wouldn't think there'd be a
> problem importing the module that defines it.

What happens if you just do 'import utilities'. Can you then call
utilities.hexdump? Can you see anything in the utilities module?

>  'hexdump': ,
>  'int.hexdump': None,
>
>
> But I suspect the first, at any rate, was loaded because of this in
> my ~/.pdbrc:
>
>   from hexdump import hexdump
>
> which wouldn't interfere when running without the debugger (that module
> in my sys.path but nowhere near my project)
>
>
> I have this in my project:
>
>   int/__init__.py
>
> But:
>
>   $ ls int/hexdump*
>   ls: cannot access int/hexdump*: No such file or directory
>
> So I don't really understand "int.hexdump".

The value of None in sys.modules caches a relative import miss. All it
means is that at some point something in the 'int' package tried to do
a relative import of the hexdump module and failed (presumably because
it doesn't exist).

Side observation: 'int' is a bad name for a package, because it will
shadow the name of the 'int' built-in.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: importing: what does "from" do?

2016-01-21 Thread Steven D'Aprano
On Fri, 22 Jan 2016 02:12 am, Charles T. Smith wrote:

> On Thu, 21 Jan 2016 07:52:17 -0700, Ian Kelly wrote:
> 
>>> I have no idea what was imported before.  I just want to import
>>> hexdump(). Unfortunately, this does not work:
>>>
>>>   from utilities import hexdump
>>>
>>> ImportError: cannot import name hexdump

Charles, according to your comments further below, you have already imported
successfully hexdump using:

from hexdump import hexdump


So why not just run that instead?



>> What is utilities? Is it a module on your sys.path?
> 
> 
> Yes.

Does utilities.py contain a function, class or other object
called "hexdump"?


(1) At the interactive interpreter, run:

import utilities
dir(utilities)

Do you see a name "hexdump" listed there? (My guess is, No.)


(2) In your text editor of choice, open the utilities.py file, and search
for "hexdump". Do you see anything by that name? (My guess is No.)


If the answer to either of those questions is Yes, then:

(3) You may be looking at a different file called "utilities.py". Is it
possible that you have two such files, and that you are looking at one, and
Python has picked up the other?

(4) Or possibly you have imported utilities into Python, THEN edited the
file and added hexdump to the file. If so, Python will not see the changes
to the file until you exit the interpreter and restart it.

(You may be able to use the reload() function to see the changes, but it is
sometimes finicky to get that to work the way people expect, it is often
better just to exit and restart.)


>> What makes you think this is caused by circular dependencies? It could
>> be, but you really haven't given us enough information about what
>> utilities is to diagnose that.
> 
> Well, I thought that that's what causes these import problems.


Circular dependencies cause SOME import problems, but not all.


> I just 
> wish that python gave me more information, like the tree of imports that's
> causing the problem.

What do you mean by "the tree of imports"? I don't even know what you might
mean by that.



>>> would you explain to me why I get this:
>>>
>>>   (PDB)hexdump(msg)
>>> *** NameError: name 'hexdump' is not defined
>> 
>> Probably because the name 'hexdump' is not defined.
> 
> 
> If indeed it's not defined, then I wouldn't think there'd be a
> problem importing the module that defines it.


I don't understand this. If hexdump isn't defined, how do you expect to
import something that doesn't exist? Or exists in a place that the
interpreter cannot see, which is effectively the same thing.



>>> P.S. can I get a list of all the loaded symbols and/or modules?

In Python 2, run this:


import __builtin__
names = vars(__builtin__).keys()
names.extend(vars().keys())
print sorted(names)


In Python 3, run this:


import builtins
names = list(vars(builtins).keys())
names.extend(vars().keys())
print(sorted(names))


To see the list of modules which have been loaded from disk and stored in
the cache (but not necessarily imported into your program), run this:

import sys
print(sys.modules)



>> What do you mean by "loaded"? Bound to a name in the main module? In the
>> globals of the current scope? In any module? Try the "dir" or "vars"
>> built-in.
>> 
>> You can get a view of all the *cached* modules by looking in the
>> sys.modules dict.
> 
> 
> as in this?
> 
>   (PDB) pp dict (sys.modules)


Charles, I **strongly** suggest that for day to day interactive exploration
of the Python environment, instead of using the debugger, you use the
ordinary (and quite powerful) Python interactive interpreter. 

At the Python prompt, just type the code you want to execute, and it will be
executed interactively.

(The one major difference between the interactive interpreter and the
non-interactive one is that the interactive interpreter is quite fussy
about blank lines between blocks. For example, you need to leave a blank
line after a function when defining it:


py> def foo():
... return 42
... x = foo()  # oops, forgot the blank line
  File "", line 3
x = foo()  # oops, forgot the blank line
^
SyntaxError: invalid syntax



and you cannot leave blank lines in the middle of a block:


py> def foo():
... a = 42
...
py> return a  # oops, the blank line has ended the block
  File "", line 1
return a  # oops, the blank line has ended the block
^
IndentationError: unexpected indent




> Okay, quite good.  I have this (among others):
> 
> 
>  'hexdump': ,
>  'int.hexdump': None,

I'm confused. You have a file hexdump, another file utilities.py containing
hexdump, and now it seems you also have a package directory "int"
containing a file called "hexadump".

How do you expect to keep track of which hexdump you are using?

Also, your package "int" is going to clash with the built-in function "int".
You should rename the package.



> But I suspect the first, at any rate, was loaded because of this in
> my ~/.pdbrc:
> 
>   from hexdump import hexdump
> 
> which w

Re: importing: what does "from" do?

2016-01-21 Thread Steven D'Aprano
On Fri, 22 Jan 2016 02:53 am, Charles T. Smith wrote:

> What I need is tools to find the problems.  In particular, why doesn't
> python give me more of a clue where the problem is?  It would be cool
> if it would tell me exactly what module to look at, which symbol(s)
> were colliding.

The problem isn't that symbols are colliding, it is that you are looking for
symbols that *don't exist* in the place you tell Python to look.


If you run 

from math import hexdump

you will get an ImportError, not because of a collision, but because there
is no math.hexdump. 

If you run

from utilities import hexdump

and Python gives you an ImportError, the most likely issue is that there is
no such name "hexdump" in that file.

Are you sure you are looking at the same file? What does this tell you?


import utilities
print(utilities.__file__)


Does it match what you expect?




-- 
Steven

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


Re: importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
On Thu, 21 Jan 2016 08:59:39 -0700, Ian Kelly wrote:

> What happens if you just do 'import utilities'. Can you then call
> utilities.hexdump? Can you see anything in the utilities module?


Yes, that works!  That's what I'm doing as a work around.
I was trying to avoid doing that because I figured it would exponentiate
my circular dependency problems and in fact, that module has no place
in the module where I need hexdump.

The obvious suggestion is to put hexdump in a file by itself, but
that's not the point, now.  The point is, how to optimally manage
(existing) complex imports.


> Side observation: 'int' is a bad name for a package, because it will
> shadow the name of the 'int' built-in.


Boy oh boy have I experienced that now!  :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: importing: what does "from" do?

2016-01-21 Thread Charles T. Smith
On Thu, 21 Jan 2016 16:30:30 +, Charles T. Smith wrote:

>> Side observation: 'int' is a bad name for a package, because it will
>> shadow the name of the 'int' built-in.
> 
> 
> Boy oh boy have I experienced that now!  :)

(it wasn't me! ;) )
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use the docstring in this property example

2016-01-21 Thread Peter Otten
Robert wrote:

> Hi,
> 
> I read below code snippet on link:
> https://docs.python.org/2/library/functions.html#property
> 
> --
> class C(object):
> def __init__(self):
> self._x = None
> 
> def getx(self):
> return self._x
> 
> def setx(self, value):
> self._x = value
> 
> def delx(self):
> del self._x
> 
> x = property(getx, setx, delx, "I'm the 'x' property.")
> If c is an instance of C, c.x will invoke the getter, c.x = value will
> invoke the setter and del c.x the deleter.
> 
> If given, doc will be the docstring of the property attribute.
> 
> 
> I can use these:
> c.x
> c.x=42
> del c.x
> 
> but I don't know how to get the doctring from the property attribute.
> Could you show me how to do that?

>>> c = C()
>>> type(c).x.__doc__
"I'm the 'x' property."

But usually you'll see it as part of the help on c:

>>> help(c)
Help on C in module __main__ object:

class C(__builtin__.object)
[...]
 |  Data descriptors defined here:
[...]
 |  x
 |  I'm the 'x' property.



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


Re: How to simulate C style integer division?

2016-01-21 Thread Random832
On Thu, Jan 21, 2016, at 09:31, Marko Rauhamaa wrote:
> Maybe:
> 
>def intdiv(a, b):
>return a // b if (a < 0) == (b < 0) else -(-a // b)

Personally, I like a // b + (a % b and a ^ b < 0) - I've done the
opposite in C to get python-style division.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Marko Rauhamaa
Random832 :

> On Thu, Jan 21, 2016, at 09:31, Marko Rauhamaa wrote:
>> Maybe:
>> 
>>def intdiv(a, b):
>>return a // b if (a < 0) == (b < 0) else -(-a // b)
>
> Personally, I like a // b + (a % b and a ^ b < 0) - I've done the
> opposite in C to get python-style division.

Well, then there's:

def intdiv(a, b):
return int(a / b)


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


Re: How to simulate C style integer division?

2016-01-21 Thread Matt Wheeler
On 21 January 2016 at 21:17, Marko Rauhamaa  wrote:
> Well, then there's:
>
> def intdiv(a, b):
> return int(a / b)

That depends on how accurate you need it to be

>>> def intdiv(a, b):
...  return a//b if (a < 0) == (b < 0) else -(-a//b)
...
>>> num = 3**171
>>> num
3870210234510307998744588107535211184800325224934979257430349324033792477926791547
>>> num2 = 4**80
>>> num2
1461501637330902918203684832716283019655932542976
>>> intdiv(num, num2)
2648105301871818722187687529062555
>>> int(num/num2)
2648105301871818477801931416797184



-- 
Matt Wheeler
http://funkyh.at
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to simulate C style integer division?

2016-01-21 Thread Grobu

On 21/01/16 09:39, Shiyao Ma wrote:

Hi,

I wanna simulate C style integer division in Python3.

So far what I've got is:
# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
   result = math.floor(result)
else:
   result = math.ceil(result)


I found it's too laborious. Any quick way?



math.trunc( float(a) / b )

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


Re: How to simulate C style integer division?

2016-01-21 Thread Terry Reedy

On 1/21/2016 3:39 AM, Shiyao Ma wrote:


I wanna simulate C style integer division in Python3.


There are two problems with this spec: it assumes that 'C style integer 
division' is well defined and that we know the definition.  Better:


"How do I write a function 'div' in Python 3 so that the following (with 
values added after each '==') is True:


all((div(1,2) == , div(-1,-2) == , dif(-1,2) == , dif(1,-2) == ))"

For '//', the values would be 0, 0, -1, -1.  If you want 0, 0, 0, 0 
(truncation toward 0), then


def div(i, j):
return (i // j) + ((i < 0) ^ (j <  0))

print(all((div(1,2) == 0, div(-1,-2) == 0,
   div(-1,2) == 0, div(1,-2) == 0)))

prints 'True'.

--
Terry Jan Reedy

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


Re: How to simulate C style integer division?

2016-01-21 Thread Terry Reedy

On 1/21/2016 9:56 PM, Terry Reedy wrote:

On 1/21/2016 3:39 AM, Shiyao Ma wrote:


I wanna simulate C style integer division in Python3.


There are two problems with this spec: it assumes that 'C style integer
division' is well defined and that we know the definition.  Better:

"How do I write a function 'div' in Python 3 so that the following (with
values added after each '==') is True:

all((div(1,2) == , div(-1,-2) == , dif(-1,2) == , dif(1,-2) == ))"

For '//', the values would be 0, 0, -1, -1.  If you want 0, 0, 0, 0
(truncation toward 0), then

def div(i, j):
 return (i // j) + ((i < 0) ^ (j <  0))

print(all((div(1,2) == 0, div(-1,-2) == 0,
div(-1,2) == 0, div(1,-2) == 0)))

prints 'True'.


But fails with remainder 0, as others noted.  Lesson: include corner 
cases in validation test.  Anyway, my main point about writing a clear 
spec remains true.


--
Terry Jan Reedy

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


Re: How to simulate C style integer division?

2016-01-21 Thread Steven D'Aprano
On Fri, 22 Jan 2016 12:59 pm, Grobu wrote:

> On 21/01/16 09:39, Shiyao Ma wrote:
>> Hi,
>>
>> I wanna simulate C style integer division in Python3.
>>
>> So far what I've got is:
>> # a, b = 3, 4
>>
>> import math
>> result = float(a) / b
>> if result > 0:
>>result = math.floor(result)
>> else:
>>result = math.ceil(result)
>>
>>
>> I found it's too laborious. Any quick way?
>>
> 
> math.trunc( float(a) / b )


That fails for sufficiently big numbers:


py> a = 3**1000 * 2
py> b = 3**1000
py> float(a)/b  # Exact answer should be 2
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to float


Note that Python gets the integer division correct:

py> a//b
2L


And even gets true division correct:

py> from __future__ import division
py> a/b
2.0


so it's just the intermediate conversion to float that fails.



-- 
Steven

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


Re: Installing on linux - missing devel packages

2016-01-21 Thread Frank Millman

On 2016-01-21, Chris Angelico  wrote:

On Thu, Jan 21, 2016 at 6:18 PM, Frank Millman  wrote:

Fedora 22 comes standard with Python 3.4.2. I want to install 3.5.1.

It is easy enough to download the source and run ./configure;make;make
altinstall. But then I find that I cannot import gzip because zlib-devel 
is

missing. I fix that, then I find that sqlite-devel is missing.

Is there an easy way to find out all the missing components, so that when
the installation is complete I can be sure I have the entire standard 
lib?


This is a Linux packaging question, more than a Python one. On Debian
systems, the way to do that is "apt-get build-dep python3"; check your
own package manager for an equivalent - it'll probably be called
builddep or similar.


Thanks for all the replies.

Frank


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


Re: How to simulate C style integer division?

2016-01-21 Thread Random832
Terry Reedy  writes:
> But fails with remainder 0, as others noted.  Lesson: include corner
> cases in validation test.  Anyway, my main point about writing a clear
> spec remains true.

My validation test for this was to loop both numerator and denominator
from -5 to 5 (skipping denominator 0) to cover all cases (comparing
against the original post's unoptimized code) without having to think
about a minimal set to cover them.

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


Re: importing: what does "from" do?

2016-01-21 Thread Rustom Mody
On Thursday, January 21, 2016 at 7:53:07 PM UTC+5:30, Charles T. Smith wrote:
> What does "from (module) import (func)" do?
> 
> Please don't tell me that I shouldn't ask because real programmers
> know not to have circular dependencies ...
> 
> I have no idea what was imported before.  I just want to import hexdump().
> Unfortunately, this does not work:
> 
>   from utilities import hexdump
> 
> ImportError: cannot import name hexdump
> 
> Now, I shouldn't have a problem because the programmers before me didn't
> understand either, and so just duplicated it everywhere, but I'd really
> like to do it right.
> 
> I would have thought that "from" would allow me to import just one
> symbol.  Now, for all the people just chomping at the bit to tell
> me about circular dependencies, would you explain to me why I get this:
> 
>   (PDB)hexdump(msg)
> *** NameError: name 'hexdump' is not defined
> 
> 
> P.S. can I get a list of all the loaded symbols and/or modules?

You may find this strategy useful:
https://pchiusano.github.io/2015-04-23/unison-update7.html

particularly the section "Limitations of refactoring via modifying text files 
in place, and what to do instead"

Note it is not python based but the explanation should be useful anyway
-- 
https://mail.python.org/mailman/listinfo/python-list


Refactoring in a large code base (was: importing: what does "from" do?)

2016-01-21 Thread Ben Finney
Rustom Mody  writes:

> You may find this strategy useful:
> https://pchiusano.github.io/2015-04-23/unison-update7.html
>
> particularly the section "Limitations of refactoring via modifying
> text files in place, and what to do instead"

A direct link to that section is
https://pchiusano.github.io/2015-04-23/unison-update7.html#refactoring-lessons>.

The author points out there are times when a code base is large and
complex enough that refactoring puts the programmer in a state of not
knowing whether they're making progress, because until the whole
refactoring is complete the errors just cascade and it's hard to tell
which ones are relevant.

That’s bad for two reasons. Without this feedback, you may be
writing code that is making things worse, not better, building
further on faulty assumptions. But more than the technical
difficulties, working in this state is demoralizing, and it kills
focus and productivity.

All right, so what do we do instead? Should we just avoid even
considering any codebase transformations that are intractable with
the “edit and fix errors” approach? No, that’s too conservative.
Instead, we just have to *avoid modifying our program in place*.
This lets us make absolutely any codebase transformation while
keeping the codebase compiling at all times. Here’s a procedure,
it’s quite simple […]


https://pchiusano.github.io/2015-04-23/unison-update7.html#refactoring-lessons>

The technique is not presented as flawless, and interesting trade-offs
are discussed. Quite an interesting article.

-- 
 \   “Science and religion are incompatible in the same sense that |
  `\   the serious pursuit of knowledge of reality is incompatible |
_o__)   with bullshit.” —Paul Z. Myers, 2010-03-14 |
Ben Finney

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