Re: is_ as method or property?
Mateusz Loskot wrote: > On 12 December 2014 at 12:26, Chris Angelico wrote: >> On Fri, Dec 12, 2014 at 10:21 PM, Mateusz Loskot >> wrote: >>> I've got several cases which are not obvious to me. >>> For instance, class Foo has a boolean attribute, read-write, >>> which I see a couple of realisations for possible: [...] > I mentioned, setting the new value involves more changes to Foo() > instance, so i's not possible to capture it with just an assignment. > Hence, the discussion between property vs method. If the calculation is cheap and fast and "feels like" getting/setting an attribute, then use property. If the calculation is expensive, or might fail, then use explicit getter/setter methods. I'm afraid that there is no objective way to tell when something "feels like" an attribute. That depends partly on the class you are dealing with, and partly on personal taste. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: list comprehension return a list and sum over in loop
On 13/12/2014 03:04, KK Sasa wrote: Sorry, i should say I'm using pythonxy, maybe it imports other things. That is good to know but without any context it's rather difficult to relate it to anything. Some people may have photographic memories and so remember everything that's been said in a thread, that certainly doesn't apply to me, and right now I'm just too lazy to go back and find out what this relates to :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
Nelson Crosby wrote: > I was thinking a bit about the following pattern: > > value = get_some_value() > while value in undesired_values: > value = get_some_value() > > I've always hated code that looks like this. Partly due to the repetition, > but partly also due to the fact that without being able to immediately > recognise this pattern, it isn't very readable. I agree! There are two fundamental indefinite loop structure: - loop zero or more times - loop one or more times Python has syntax to support the first, namely `while`, but doesn't have syntax to support the second. Now it is certainly true that not everything needs to be syntax. You can easily adjust a `while` loop to behave like a one-or-more loop. One way is to use a sentinel value that is guaranteed to be in the undesired set: value = something_undesired while value in undesired_values: value = get_some_value() Another way is to use an infinite loop and then break out after at least one cycle: while True: value = get_some_value() if value in undesired_values: break Both of these can be good enough. A good programmer should know these techniques because they can be generalised to "loop and a half": while True: first_half() if condition: break second_half() and any other semantics you might like. (This suggestions that there is, in fact, only one *fundamental* indefinite loop: the infinite loop.) But neither is elegant and neither reads like English pseudo-code. Pascal has syntax for one-or-more loops, and inspired by that Python might have had something like this: repeat: until condition That however would require two new keywords (repeat and until), and the benefit is not enough to make it worth breaking all the code that already uses "repeat" as a variable. > Python already has one-line syntaxes (e.g. list comprehensions), I was > wondering what people thought about a similar thing with while. It might > look something like: > > value = get_some_value() while value in undesired_values() That is no help at all for the general case where you have a block of code inside the while loop. It certainly isn't worth having syntax for such a special case. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: list comprehension return a list and sum over in loop
KK Sasa writes: > Hi there, > > The list comprehension is results = [d2(t[k]) for k in > xrange(1000)], where d2 is a function returning a list, say > [x1,x2,x3,x4] for one example. So "results" is a list consisting of > 1000 lists, each of length four. Here, what I want to get is the sum > of 1000 lists, and then the result is a list of length four. Is > there any efficient way to do this? Because I found it is slow in my > case. I tried sum(d2(t[k]) for k in xrange(1000)), but it returned > error: TypeError: unsupported operand type(s) for +: 'int' and > 'list'. Thanks. Why didn't you follow Mark Lawrence's advice? In your problem, results is a list of N sublists, each containing exactly four numerical values, Let's try with N=2 In [36]: results = [d2(t[k]) for k in range(2)] In [37]: print results [[1, 2, 3, 4], [5, 6, 7, 8]] Let's try the obvious method to sum In [38]: [sum(el) for el in results] Out[38]: [10, 26] not what you're looking for, but what if we had In [39]: [sum(el) for el in zip(*results)] Out[39]: [6, 8, 10, 12] correct. BTW, as you're using the scientific stack the answer of Christian Gollwitzer is the more appropriate. -- https://mail.python.org/mailman/listinfo/python-list
Re: Creating interactive command-line Python app?
um, what if I want to USE a command line for python WITHOUT downloading or installing it -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 2.x and 3.x use survey, 2014 edition
On Thu, Dec 11, 2014 at 10:32 PM, Ben Finney wrote: > > "Giampaolo Rodola'" writes: > > > I still think the only *real* obstacle remains the lack of important > > packages such as twisted, gevent and pika which haven't been ported > > yet. > > What disqualifies other obstacles from being “*real* obstacles”? How do > you determine that? > > > With those ones ported switching to Python 3 *right now* is not only > > possible and relatively easy, but also convenient. > > If my program relies on an obscure library ‘foo’, and that library is > not ported to Python 3, switching to Python 3 is not feasible, and > certainly not convenient — regardless of the status of “important > packages such as twisted, gevent, and pika”. > > So your assertion here is plainly false. > > What is it you're actually wanting to say? What I'm saying is that for a very long time a considerable number of libraries haven't been ported to python 3 and that lasted for years, say until Python 3.3, or until Django started supporting Python 3, which happened less than a year ago. Names such as Twisted, gevent, eventlet, python-daemon and paramiko means that literally hundreds of thousands of users cannot even think about migrating *right now*: they're just stuck. My perception is that for way too long Python 3 was kind of ignored by some of the most important library vendors and it (partially) still is, and that's what is affecting Python 3 adoption the most. Try to take a look at how many missing dependencies a project such as OpenStack still has: http://stackoverflow.com/questions/20075574/finding-which-packages-support-python-3-x-vs-2-7-x/22113627#22113627. OpenStack is probably a bad example 'cause it's *huge*, but when the missing deps have hundreds of thousands of user (see https://python3wos.appspot.com/) it is very easy to hit one of those and remain stuck even if your project is a lot smaller. I personally tried to migrate 2 medium-sized (> 10.000 LOC) projects at work for 2 different companies and what I ended up doing was modernizing the code so that both 2.7 and 3.3+ interpreters could execute it and tests didn't raise any "python-related error" (SyntaxError, TypeError, UnicodeError or whatever), but I punctually hit the wall of 1, 2 or 3 missing dependencies. So basically both of these 2 projects are "python 3 ready" but since the library ecosystem is not I just "wait" (it's been a year now). -- Giampaolo - http://grodola.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Creating interactive command-line Python app?
[email protected] wrote: > um, what if I want to USE a command line for python WITHOUT downloading or > installing it Who are you talking to? What is the context? Like all software, you can't use Python apps without all their dependencies being installed. If you use the Linux operating system, it will have Python already installed. Otherwise, you will have to install it. If you can't install it, or don't want to, you can't use Python. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 2.x and 3.x use survey, 2014 edition
"Giampaolo Rodola'" : > What I'm saying is that for a very long time a considerable number of > libraries haven't been ported to python 3 Ok, that's at least half the fault of the library developers. > Names such as Twisted, gevent, eventlet, python-daemon and paramiko > means that literally hundreds of thousands of users cannot even think > about migrating *right now*: they're just stuck. I *have* always been very suspicious of third-party libraries and platforms. The comes-with-batteries approach has been enough for my needs. > My perception is that for way too long Python 3 was kind of ignored by > some of the most important library vendors and it (partially) still > is, and that's what is affecting Python 3 adoption the most. Maybe. I'm not afraid of the Python 3 transition, but it's not current yet. I'm running a Python 3 application at home. At work, Python 2.3 is the version in one environment, Python 2.6 in another. When Python 3.x becomes the least common denominator, we'll build on that. Some other teams have already made the jump. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 2.x and 3.x use survey, 2014 edition
Marko Rauhamaa wrote: > At work, Python 2.3 is the version in one environment Good grief! What's the OS you are using for that? -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 2.x and 3.x use survey, 2014 edition
Steven D'Aprano : > Marko Rauhamaa wrote: > >> At work, Python 2.3 is the version in one environment > > Good grief! What's the OS you are using for that? RHEL 4. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: [Python-Dev] Python 2.x and 3.x use survey, 2014 edition
On 2014-12-10, Bruno Cauet wrote: > Nathaniel, I'm not sure about that: even if the code is 2- and 3-compatible > you'll pick one runtime. Why do you say that? I have both installed. I use both. Sometimes it depends on which OS/distro I'm running, sometimes other reasons prevail. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Creating interactive command-line Python app?
Steven D'Aprano writes: > [email protected] wrote: > >> um, what if I want to USE a command line for python WITHOUT downloading or >> installing it > > Who are you talking to? What is the context? > > Like all software, you can't use Python apps without all their dependencies > being installed. If you use the Linux operating system, it will have Python > already installed. Otherwise, you will have to install it. > > If you can't install it, or don't want to, you can't use Python. cx_Freeze, PyInstaller, py2exe, etc allow to create a standalone distribution i.e., you could ship your executable with a bundled Python interpreter. -- Akira -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Script to convert firewall rules
On Saturday, December 13, 2014 6:31:34 AM UTC+4, Jason Friedman wrote:
> Thanks for the reply. Yes I can make the all possible keywords/values for
> both formate. But after that what gonna be the logic to convert one format to
> other format. Like to convert one line below are the keywords:
>
>
>
> set interface ethernet2/5 ip 10.17.10.1/24 (format 1)
>
> set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24 (format 2)
>
>
>
> (set, interface, ip) = (set, interfaces, family inet address)
>
>
>
> But some values are variable and should ask the user to convert manually like
> ethernet2/5 equal to ge-0/0/0 or ge-0/0/1 or ge-0/0/2
>
>
>
> And some values keep as it is like 10.17.10.1/24
>
>
>
> Also then format 2 can be converted int o format 3 (as below) for more
> readability of format 2. This is just optional.
>
>
>
> interfaces {
>
> ge-2/0/5 {
>
> unit 0 {
>
> family inet {
>
> address 10.17.10.1/24;
>
> }
>
> }
>
> }
>
> }
>
>
>
>
> Note that the practice on this list is to put your response after the
> (edited) portion of the previous posts.
>
>
> Are you willing to learn some Python, if someone gets you started?
>
> Would it be helpful if someone provided Python code to convert this:
>
> set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24
>
>
> to this:
>
>
> interfaces {
>
> ge-2/0/5 {
>
> unit 0 {
>
> family inet {
>
> address 10.17.10.1/24;
>
> }
>
> }
>
> }
>
> }
>
>
> ?
Hello
Thanks for the reply. I am learning python using CBT nuggets for python. But If
you can refer me some good course, that should be practical then it would be
great.
For my requirement, if you can give me the best approach to start with or high
level steps or give me some sample cod, I really appreciate that.
Regards,
Kashif
--
https://mail.python.org/mailman/listinfo/python-list
Re: [Python-Dev] Python 2.x and 3.x use survey, 2014 edition
On Sat, Dec 13, 2014 at 9:51 AM, Grant Edwards wrote: > On 2014-12-10, Bruno Cauet wrote: > >> Nathaniel, I'm not sure about that: even if the code is 2- and 3-compatible >> you'll pick one runtime. > > Why do you say that? > > I have both installed. I use both. Sometimes it depends on which > OS/distro I'm running, sometimes other reasons prevail. Just to give another anecdote, I wrote myself a little tool last night for visualizing healthcare scenarios to help my family decide which insurance plan to choose this year. I didn't realize until I added 'subTest's to the tests and mistakenly invoked them as "python -m test" instead of "python3 -m test" that I'd accidentally written it to be 2/3 compatible! I took the subTest back out, and tests pass with both interpreters. -- Zach (If such a tool could be useful to anyone, I can post it on BitBucket/GitHub. Its support for all possibilities is far from complete, but it helped us a bit. Also, I make no guarantees that you won't want to gouge your eyes out reading the code, but that *shouldn't* happen ;) -- https://mail.python.org/mailman/listinfo/python-list
numpy question (fairly basic, I think)
Hi, I am new to numpy. I am reading binary data one record at a time (I have to) and I would like to store all the records in a numpy array which I pre-allocate. Below I try to fill the empty array with exactly one record, but it is filled with as many rows as there are columns. Why is this? It is probably something simple, but I am stuck! It is like the original record is not unpacked *as in tuple unpacking) into the array, so it remains one chunk, not an (nrows, ncols) structure. from __future__ import print_function import numpy as np # one binary record s = '\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00;@\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x80U\xe1@\x00\x00\x00\x00\x80\xd9\xe4@\x00\x00\x00\x00@\xa7\xe3@\xab\xaa\xaa\xaajG\xe3@\x00\x00\x00\x00\x80\xd9\xe4@\x00\x00\x00\x00\x00\x00;@\x00\x00\x00\x00\x00\x00;@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\xa4DI\tBx qwertyuiopasdfghjklzxcvbnm,./ \x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00p\x9f@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00(@DEC 2012' # read it into a structured array formats = ['https://mail.python.org/mailman/listinfo/python-list
Re: Creating interactive command-line Python app?
Akira Li wrote: > Steven D'Aprano writes: > >> [email protected] wrote: >> >>> um, what if I want to USE a command line for python WITHOUT downloading >>> or installing it >> >> Who are you talking to? What is the context? >> >> Like all software, you can't use Python apps without all their >> dependencies being installed. If you use the Linux operating system, it >> will have Python already installed. Otherwise, you will have to install >> it. >> >> If you can't install it, or don't want to, you can't use Python. > > cx_Freeze, PyInstaller, py2exe, etc allow to create a standalone > distribution i.e., you could ship your executable with a bundled Python > interpreter. You still have to download and install the application. Sounds to me like the OP wants to use the Python interactive interpreter without installing Python. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: numpy question (fairly basic, I think)
Albert-Jan Roskam wrote: > Hi, > > I am new to numpy. I am reading binary data one record at a time (I have > to) and I would like to store all the records in a numpy array which I > pre-allocate. Below I try to fill the empty array with exactly one record, > but it is filled with as many rows as there are columns. Why is this? It > is probably something simple, but I am stuck! It is like the original > record is not unpacked *as in tuple unpacking) into the array, so it > remains one chunk, not an (nrows, ncols) structure. Can you simplify the example to something shorter that focuses on the issue at hand? It isn't clear to me which bits of the code you show are behaving the way you expect and which bits are not. To get you started, here is what I got working: import numpy as np # one binary record s = '\x00\x01\x00\xff'*2 # eight bytes makes one C double # read it into a structured array formats = ['https://mail.python.org/mailman/listinfo/python-list
Re: [Python-Dev] Python 2.x and 3.x use survey, 2014 edition
On 13 Dec 2014 05:19, "Petr Viktorin" wrote: > > Also keep in mind that not all Python libraries are on PyPI. > For non-Python projects with Python bindings (think video players, > OpenCV, systemd, Samba), distribution via PyPI doesn't make much > sense. And since the Python bindings are usually second-class > citizens, the porting doesn't have a high priority. > > If anyone is wondering why their favorite Linux distribution is stuck > with Python 2 – well, I can only speak for Fedora, but nowadays most > of what's left are CPython bindings. > No pylint --py3k or 2to3 will help there... That's a good point. I actually think https://docs.python.org/3/howto/cporting.html#cporting-howto is actually in a worse state than the state the Python level porting guide was in until Brett's latest round of updates, as it covers the underlying technical details of the incompatibilities moreso than the available tools and recommended processes for *executing* a migration. For example, replacing a handcrafted Python extension with a normal C library plus cffi, Cython or SWIG generated Python bindings can deliver both an easier to maintain extension *and* Python 3 compatibility. Similarly, converting an extension from C to Cython outright (without a separate C library) can provide both benefits. It's mainly when converting to one of those isn't desirable and/or feasible that you really need to worry about C API level porting. For that, tools like Dave Malcolm's static CPython extension analyser for gcc could potentially be helpful (as pylint was to Brett's update to the Python level guide), and Lennart also provides some more detailed practical suggestions in http://python3porting.com/cextensions.html I'm sure there are other useful techniques that can be employed, but aren't necessarily well known outside the folks that have been busy implementing these migrations. Barry, Petr, any of the other folks working on distro level C extension ports, perhaps one of you would be willing to consider an update to the C extension porting guide to be more in line with Brett's latest version of the Python level porting guide? Regards, Nick. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Script to convert firewall rules
> Thanks for the reply. I am learning python using CBT nuggets for python. But
> If you can refer me some good course, that should be practical then it would
> be great.
>
> For my requirement, if you can give me the best approach to start with or
> high level steps or give me some sample cod, I really appreciate that.
>
Good, some other sources for learning:
https://docs.python.org/3/tutorial/
http://learnpythonthehardway.org/
Here's some code to get you started (version 3.4.0):
"""
convert
set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24
to
interfaces {
ge-2/0/5 {
unit 0 {
family inet {
address 10.17.10.1/24;
}
}
}
}
"""
class interface():
attribute_name_list = ("ge", "unit", "family", "address")
def __init__(self, ge, unit, family, address):
self.ge = ge
self.unit = unit
self.family = family
self.address = address
def convert(interface_list, indent=4):
indentation = 0
return_list = list()
return_list.append(" " * indentation + "interfaces {")
for interface in interface_list:
for attribute_name in interface.attribute_name_list:
indentation += indent
text = "%s %s {" % (attribute_name, getattr(interface,
attribute_name))
return_list.append(" " * indentation + text)
while indentation > indent:
indentation -= indent
return_list.append(" " * indentation + "}")
indentation -= indent
return_list.append("}")
return "\n".join(return_list)
if __name__ == "__main__":
interface1 = interface("0/0/0", "0", "inet", "10.17.10.1/24")
interface2 = interface("2/0/5", "0", "inet", "11.18.10.1/24")
print(convert((interface1, interface2, )))
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python Script to convert firewall rules
Hi Jason
Thank you very much. Appreciated ! But the first requirement was to convert
format1 to format2 as below:
set interface ethernet2/5 ip 10.17.10.1/24 (format 1)
set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24 (format 2)
(set, interface, ip) = (set, interfaces, family inet address)
But some values are variable and should ask the user to convert manually
like ethernet2/5 equal to ge-0/0/0 or ge-0/0/1 or ge-0/0/2
And some values keep as it is like 10.17.10.1/24
Thanks and Regards,
Kashif
On Sun, Dec 14, 2014 at 5:35 AM, Jason Friedman wrote:
>
> > Thanks for the reply. I am learning python using CBT nuggets for python.
> But If you can refer me some good course, that should be practical then it
> would be great.
> >
> > For my requirement, if you can give me the best approach to start with
> or high level steps or give me some sample cod, I really appreciate that.
> >
> Good, some other sources for learning:
> https://docs.python.org/3/tutorial/
> http://learnpythonthehardway.org/
>
> Here's some code to get you started (version 3.4.0):
>
> """
> convert
>
> set interfaces ge-0/0/0 unit 0 family inet address 10.17.10.1/24
>
> to
>
> interfaces {
> ge-2/0/5 {
> unit 0 {
> family inet {
> address 10.17.10.1/24;
> }
> }
> }
> }
> """
>
> class interface():
> attribute_name_list = ("ge", "unit", "family", "address")
> def __init__(self, ge, unit, family, address):
> self.ge = ge
> self.unit = unit
> self.family = family
> self.address = address
>
> def convert(interface_list, indent=4):
> indentation = 0
> return_list = list()
> return_list.append(" " * indentation + "interfaces {")
> for interface in interface_list:
> for attribute_name in interface.attribute_name_list:
> indentation += indent
> text = "%s %s {" % (attribute_name, getattr(interface,
> attribute_name))
> return_list.append(" " * indentation + text)
> while indentation > indent:
> indentation -= indent
> return_list.append(" " * indentation + "}")
> indentation -= indent
> return_list.append("}")
> return "\n".join(return_list)
>
> if __name__ == "__main__":
> interface1 = interface("0/0/0", "0", "inet", "10.17.10.1/24")
> interface2 = interface("2/0/5", "0", "inet", "11.18.10.1/24")
> print(convert((interface1, interface2, )))
>
--
https://mail.python.org/mailman/listinfo/python-list
