[HN] How to Make Python Wait
https://news.ycombinator.com/item?id=21834408 -- https://mail.python.org/mailman/listinfo/python-list
Re: [HN] How to Make Python Wait
On Fri, Dec 20, 2019 at 11:16 PM Pankaj Jangid wrote: > > https://news.ycombinator.com/item?id=21834408 > > Did you just post a blog article, then spam everywhere to try to get traffic, where your entire blog post is telling people worse ways to do a time.sleep()? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: [HN] How to Make Python Wait
Original from miguel grinberg -- https://mail.python.org/mailman/listinfo/python-list
Unable to install "collect" via pip3
Hi I can install collect with pip for python2.7 $ pip install --user collect Collecting collect Using cached https://files.pythonhosted.org/packages/cf/5e/c0f0f51d081665374a2c219ea4ba23fb1e179b70dded96dc16606786d828/collect-0.1.1.tar.gz Collecting couchdbkit>=0.5.7 (from collect) Using cached https://files.pythonhosted.org/packages/a1/13/9e9ff695a385c44f62b4766341b97f2bd8b596962df2a0beabf358468b70/couchdbkit-0.6.5.tar.gz Collecting restkit>=4.2.2 (from couchdbkit>=0.5.7->collect) Downloading https://files.pythonhosted.org/packages/76/b9/d90120add1be718f853c53008cf5b62d74abad1d32bd1e7097dd913ae053/restkit-4.2.2.tar.gz (1.3MB) 100% || 1.3MB 633kB/s Collecting http-parser>=0.8.3 (from restkit>=4.2.2->couchdbkit>=0.5.7->collect) Downloading https://files.pythonhosted.org/packages/07/c4/22e3c76c2313c26dd5f84f1205b916ff38ea951aab0c4544b6e2f5920d64/http-parser-0.8.3.tar.gz (83kB) 100% || 92kB 2.4MB/s Collecting socketpool>=0.5.3 (from restkit>=4.2.2->couchdbkit>=0.5.7->collect) Downloading https://files.pythonhosted.org/packages/d1/39/fae99a735227234ffec389b252c6de2bc7816bf627f56b4c558dc46c85aa/socketpool-0.5.3.tar.gz Building wheels for collected packages: collect, couchdbkit, restkit, http-parser, socketpool Running setup.py bdist_wheel for collect ... done Stored in directory: /home/mnaderan/.cache/pip/wheels/b9/7c/7c/b09b334cc0e27b4f63ee9f6f19ca1f3db8672666a7e0f3d9cd Running setup.py bdist_wheel for couchdbkit ... done Stored in directory: /home/mnaderan/.cache/pip/wheels/f6/05/1b/f8f576ef18564bc68ab6e64f405e1263448036208cafb221e0 Running setup.py bdist_wheel for restkit ... done Stored in directory: /home/mnaderan/.cache/pip/wheels/48/c5/32/d0d25fb272791a68c49c26150f332d9b9492d0bc9ea0cdd2c7 Running setup.py bdist_wheel for http-parser ... done Stored in directory: /home/mnaderan/.cache/pip/wheels/22/db/06/cb609a3345e7aa87206de160f00cc6af364650d1139d904a25 Running setup.py bdist_wheel for socketpool ... done Stored in directory: /home/mnaderan/.cache/pip/wheels/93/f6/8c/65924848766618647078cb66b1d964e8b80876536e84517469 Successfully built collect couchdbkit restkit http-parser socketpool Installing collected packages: http-parser, socketpool, restkit, couchdbkit, collect Successfully installed collect-0.1.1 couchdbkit-0.6.5 http-parser-0.8.3 restkit-4.2.2 socketpool-0.5.3 However, pip3 fails with this error $ pip3 install --user collect Collecting collect Using cached https://files.pythonhosted.org/packages/cf/5e/c0f0f51d081665374a2c219ea4ba23fb1e179b70dded96dc16606786d828/collect-0.1.1.tar.gz Collecting couchdbkit>=0.5.7 (from collect) Using cached https://files.pythonhosted.org/packages/a1/13/9e9ff695a385c44f62b4766341b97f2bd8b596962df2a0beabf358468b70/couchdbkit-0.6.5.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 1, in File "/tmp/pip-build-qf95n0tt/couchdbkit/setup.py", line 25, in long_description = file( NameError: name 'file' is not defined Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-qf95n0tt/couchdbkit/ I can not figure out what is the problem. Any way to fix that? More info: $ which python /usr/bin/python $ ls -l /usr/bin/python lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7 $ which python3 /usr/bin/python3 $ ls -l /usr/bin/python3 lrwxrwxrwx 1 root root 9 Jun 21 2018 /usr/bin/python3 -> python3.6 Regards, Mahmood -- https://mail.python.org/mailman/listinfo/python-list
Problems with "Tarfile.close()"
Greetings, One of my Python scripts basically does the following: source = tarfile.open(name=tar_archive , mode='r|*') dest = tarfile.open(fileobj=sys.stdout, mode='w|', format=fmt) . . . source.close() dest.close() In an attempt to move my Python scripts from Python 2.7 to Python 3.6 I ran into the problem that under Python 3.6 the call to "dest.close()" fails: Traceback (most recent call last): File ".../tar_archive.copy", line 137, in dest.close() File "/usr/lib64/python3.6/tarfile.py", line 1742, in close self.fileobj.close() File "/usr/lib64/python3.6/tarfile.py", line 467, in close self.fileobj.write(self.buf) TypeError: write() argument must be str, not bytes What am I doing wrong? By the way: since on some hosts this script is running on the transition from Python 2.7 to Python 3.x will not happen immediately, I need a solution which works with both versions. Sincerely, Rainer -- https://mail.python.org/mailman/listinfo/python-list
Re: Unable to install "collect" via pip3
On Sat, Dec 21, 2019 at 2:25 AM Mahmood Naderan via Python-list wrote: > > Hi > > I can install collect with pip for python2.7 > $ pip install --user collect > However, pip3 fails with this error > $ pip3 install --user collect > NameError: name 'file' is not defined > > I can not figure out what is the problem. Any way to fix that? > Are you trying to install this package? https://pypi.org/project/collect/ It's alpha software that does not claim to support Python 3. The last release was in 2011. The web site it links to is defunct. Maybe that wasn't what you intended to install? Check the spelling of the package you're trying to install - maybe it's named slightly differently. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems with "Tarfile.close()"
On Sat, Dec 21, 2019 at 2:29 AM Dr Rainer Woitok wrote: > > Greetings, > > One of my Python scripts basically does the following: > > source = tarfile.open(name=tar_archive , mode='r|*') > dest = tarfile.open(fileobj=sys.stdout, mode='w|', format=fmt) > > . > . > . > > source.close() > dest.close() > > In an attempt to move my Python scripts from Python 2.7 to Python 3.6 I > ran into the problem that under Python 3.6 the call to "dest.close()" > fails: (I think the fact that it fails when you close the file is a red herring; it would fail at some point, and it happens to hold things over until it closes.) > Traceback (most recent call last): > File ".../tar_archive.copy", line 137, in > dest.close() > File "/usr/lib64/python3.6/tarfile.py", line 1742, in close > self.fileobj.close() > File "/usr/lib64/python3.6/tarfile.py", line 467, in close > self.fileobj.write(self.buf) > TypeError: write() argument must be str, not bytes > > What am I doing wrong? By the way: since on some hosts this script is > running on the transition from Python 2.7 to Python 3.x will not happen > immediately, I need a solution which works with both versions. > Possibly the easiest fix would be to open all files in binary mode. I think your content is binary anyway by the look of it. Just add the letter "b" to the end of your open file modes, and on Py2, it'll ensure that no newline conversion happens (mainly an issue on Windows, which is why you probably don't feel the need to do this), but on Py3, it means that it expects bytestrings everywhere. If that *doesn't* work, then you may need to mark some of your strings as binary, if you have unadorned strings containing ASCII data. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems with "Tarfile.close()"
On 12/20/2019 04:19 AM, Dr Rainer Woitok wrote: One of my Python scripts basically does the following: source = tarfile.open(name=tar_archive , mode='r|*') dest = tarfile.open(fileobj=sys.stdout, mode='w|', format=fmt) . . . source.close() dest.close() In an attempt to move my Python scripts from Python 2.7 to Python 3.6 I ran into the problem that under Python 3.6 the call to "dest.close()" fails: Traceback (most recent call last): File ".../tar_archive.copy", line 137, in dest.close() File "/usr/lib64/python3.6/tarfile.py", line 1742, in close self.fileobj.close() File "/usr/lib64/python3.6/tarfile.py", line 467, in close self.fileobj.write(self.buf) TypeError: write() argument must be str, not bytes In Python 3 `sys.stdout` is a character interface, not bytes. There are a couple solutions to the Python 3 aspect of the problem here: https://stackoverflow.com/q/908331/208880 If those answers do not work on Python 2 you'll need to detect which version you are on and act appropriately, perhaps hiding that bit of complexity in a function or class. -- ~Ethan~ -- https://mail.python.org/mailman/listinfo/python-list
Re: How to improve epoll speed when recv from kernel via netlink?
> On 20 Dec 2019, at 04:33, lampahome wrote: > > I tried to receive msg from kernel via netlink of socket. > > And I use epoll to receive netlink events whenever it comes from kernel to > user space. > > But I found the performance is poor e.g. epoll costs 90% time of execution > time after I profile it by cProfile module. > > Are there any tips to improve this? cProfile is telling you how long the code is waiting for epoll to return. It is not telling that epoll is a problem. Barry > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: [HN] How to Make Python Wait
>> https://news.ycombinator.com/item?id=21834408 > Did you just post a blog article, then spam everywhere to try to get > traffic, where your entire blog post is telling people worse ways to > do a time.sleep()? Blog post is not mine. I have a habit of posting good things to HN. When I got notification that it is getting good traction, people are discussing, then I posted the discussion link here. If that is not liked here then I won't post again. -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems with "Tarfile.close()"
Ethan, On Friday, 2019-12-20 07:41:51 -0800, you wrote: > ... > In Python 3 `sys.stdout` is a character interface, not bytes. Does that mean that with Python 3 "Tarfile" is no longer able to write the "tar" file to a pipe? Or is there now another way to write to a pipe? And if that new way also worked with Python 2, it would be even better ... :-) > There are a couple solutions to the Python 3 aspect of the problem here: > >https://stackoverflow.com/q/908331/208880 Using "sys.stdout.buffer" seems to work in Python 3 (at least with my current rather trivial test case) but does not work in Python 2. Quest- ion: what is the cheapest way to retrieve the Python version the script is executing in? Sincerely, Rainer -- https://mail.python.org/mailman/listinfo/python-list
Re: on sorting things
Eli the Bearded wrote: > In comp.lang.python, Peter Otten <__pete...@web.de> wrote: >> Eli the Bearded wrote: >>> But what caught my eye most, as someone relatively new to Python but >>> with long experience in C in Perl, is sorting doesn't take a > > s/C in /C and/ > > Ugh. > >>> *comparison* function, it takes a *key generator* function, and that >>> function is supposed to transform the thing into something that the >>> native comparison knows how to compare. >>> >>> This seems a strange choice, and I'm wondering if someone can explain >>> the benefits of doing it that way to me. >> >> Python 2 started with a comparison function and then grew a key function. >> With a key function you still have to compare items, you are just >> breaking the comparison into two steps: > > [snip] > > Thanks for that good explanation. The benchmark comparison makes it > very thorough. > > In my mind I gravitate towards the complicated sorts of sort that can be > quickly compared for some sorts of keys and not as quickly for others. > > Consider a sort that first compares file size and if the same number of > bytes, then compares file checksum. Any decently scaled real world > implementation would memoize the checksum for speed, but only work it out > for files that do not have a unique file size. The key method requires > it worked out in advance for everything. Oscar already mentioned the functools.cmp_to_key decorator which makes this a non-issue: def mycmp(a, b): ... files.sort(key=cmp_to_key(mycmp)) Applied to your example, with memoization: # untested def cmp(a, b): return (a > b)-(a < b) def make_file_key(): size = functools.lru_cache(None)(getsize) checksum = functools.lru_cache(None)(getchecksum) @functools.cmp_to_key def file_key(a, b): return cmp(size(a), size(b)) or cmp(checksum(a), checksum(b)) return file_key files.sort(key=make_file_key()) > But I see the key method handles the memoization under the hood for you, > so those simpler, more common sorts of sort get an easy to see benefit. > > Elijah > -- > even memoizing the stat() calls would help for large lists PS: If you are sorting files by size and checksum as part of a deduplication effort consider using dict-s instead: def grouped(items, key): result = defaultdict(list) for item in items: result[key(item)].append(item) return result for same_size in grouped(files, key=getsize).values(): if len(same_size) > 1: for same_checksum in grouped(same_size, key=getchecksum).values(): if len(same_checksum) > 1: print(same_checksum) -- https://mail.python.org/mailman/listinfo/python-list
Re: on sorting things
On Sat, Dec 21, 2019 at 5:03 AM Peter Otten <__pete...@web.de> wrote: > PS: If you are sorting files by size and checksum as part of a deduplication > effort consider using dict-s instead: Yeah, I'd agree if that's the purpose. But let's say the point is to have a guaranteed-stable ordering of files that are primarily to be sorted by file size - in order to ensure that two files are in the same order every time you refresh the view, they get sorted by their checksums. There ARE good reasons to do weird things with sorting, and a custom key object (either with cmp_to_key or directly implemented) can do that. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Cython, producing different modules from the same .pyx
Greg Ewing writes: > You could try creating a set of top-level .pyx stubs, each of > which just 'include' the real code. Thank you, will try this approach! ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. l...@metapensiero.it | -- Fortunato Depero, 1929. -- https://mail.python.org/mailman/listinfo/python-list
Re: Cython, producing different modules from the same .pyx
Ethan Furman writes: > If you don't get an answer here, you can try the Cython Users group: Thanks, reposted the same question there. ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. l...@metapensiero.it | -- Fortunato Depero, 1929. -- https://mail.python.org/mailman/listinfo/python-list
Re: on sorting things
Chris Angelico wrote: > On Sat, Dec 21, 2019 at 5:03 AM Peter Otten <__pete...@web.de> wrote: >> PS: If you are sorting files by size and checksum as part of a >> deduplication effort consider using dict-s instead: > > Yeah, I'd agree if that's the purpose. But let's say the point is to > have a guaranteed-stable ordering of files that are primarily to be > sorted by file size - in order to ensure that two files are in the > same order every time you refresh the view, they get sorted by their > checksums. One thing that struck me about Eli's example is that it features two key functions rather than a complex comparison. If sort() would accept a sequence of key functions each function could be used to sort slices that compare equal when using the previous key. > There ARE good reasons to do weird things with sorting, and a custom > key object (either with cmp_to_key or directly implemented) can do > that. Indeed. -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems with "Tarfile.close()"
Dr Rainer Woitok wrote: > Ethan, > > On Friday, 2019-12-20 07:41:51 -0800, you wrote: > >> ... >> In Python 3 `sys.stdout` is a character interface, not bytes. > > Does that mean that with Python 3 "Tarfile" is no longer able to write > the "tar" file to a pipe? Or is there now another way to write to a > pipe? And if that new way also worked with Python 2, it would be even > better ... :-) > >> There are a couple solutions to the Python 3 aspect of the problem here: >> >>https://stackoverflow.com/q/908331/208880 > > Using "sys.stdout.buffer" seems to work in Python 3 (at least with my > current rather trivial test case) but does not work in Python 2. Quest- > ion: what is the cheapest way to retrieve the Python version the script > is executing in? While I didn't look into the stackoverflow page an easy way to get something that accepts bytes may be # untested stdout = sys.stdout try: stdout = stdout.buffer except AttributeError: pass tarfile.open(fileobj=stdout, ...) -- https://mail.python.org/mailman/listinfo/python-list
Re: on sorting things
On Sat, Dec 21, 2019 at 6:01 AM Peter Otten <__pete...@web.de> wrote: > > Chris Angelico wrote: > > > On Sat, Dec 21, 2019 at 5:03 AM Peter Otten <__pete...@web.de> wrote: > >> PS: If you are sorting files by size and checksum as part of a > >> deduplication effort consider using dict-s instead: > > > > Yeah, I'd agree if that's the purpose. But let's say the point is to > > have a guaranteed-stable ordering of files that are primarily to be > > sorted by file size - in order to ensure that two files are in the > > same order every time you refresh the view, they get sorted by their > > checksums. > > One thing that struck me about Eli's example is that it features two key > functions rather than a complex comparison. > > If sort() would accept a sequence of key functions each function could be > used to sort slices that compare equal when using the previous key. > That would basically make it a comparison function, not a key function :) The value of the key function is that it's called exactly once per element, and the result is what you sort by. There's no correlation to any other element. It's effectively this: # sortme.sort(key=keyfunc) keys = sortme.map(keyfunc) keys.sort(keep_in_parallel=sortme) Which is why cmp_to_key is the correct solution to that problem. (For cases where you don't mind pre-calling both key functions, of course, it's equivalent to a single key function that returns a tuple.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems with "Tarfile.close()"
On Sat, Dec 21, 2019 at 6:13 AM Peter Otten <__pete...@web.de> wrote: > > Dr Rainer Woitok wrote: > > > Ethan, > > > > On Friday, 2019-12-20 07:41:51 -0800, you wrote: > > > >> ... > >> In Python 3 `sys.stdout` is a character interface, not bytes. > > > > Does that mean that with Python 3 "Tarfile" is no longer able to write > > the "tar" file to a pipe? Or is there now another way to write to a > > pipe? And if that new way also worked with Python 2, it would be even > > better ... :-) > > > >> There are a couple solutions to the Python 3 aspect of the problem here: > >> > >>https://stackoverflow.com/q/908331/208880 > > > > Using "sys.stdout.buffer" seems to work in Python 3 (at least with my > > current rather trivial test case) but does not work in Python 2. Quest- > > ion: what is the cheapest way to retrieve the Python version the script > > is executing in? > > While I didn't look into the stackoverflow page an easy way to get something > that accepts bytes may be > > # untested > stdout = sys.stdout > try: > stdout = stdout.buffer > except AttributeError: > pass This construct can be simplified down to: stdout = getattr(sys.stdout, "buffer", sys.stdout) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Unable to install "collect" via pip3
On 12/20/2019 2:22 AM, Mahmood Naderan via Python-list wrote: However, pip3 fails with this error $ pip3 install --user collect Collecting collect Using cached https://files.pythonhosted.org/packages/cf/5e/c0f0f51d081665374a2c219ea4ba23fb1e179b70dded96dc16606786d828/collect-0.1.1.tar.gz Collecting couchdbkit>=0.5.7 (from collect) Using cached https://files.pythonhosted.org/packages/a1/13/9e9ff695a385c44f62b4766341b97f2bd8b596962df2a0beabf358468b70/couchdbkit-0.6.5.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 1, in File "/tmp/pip-build-qf95n0tt/couchdbkit/setup.py", line 25, in long_description = file( NameError: name 'file' is not defined The builtin function 'file' does not exist in 3.x. pip3 is trying to install 2.x code. I suspect this is because the package is not properly labelled as 2.x only. (Some 2.x code will run unaltered on 3.x, so pip should try even if 3.x is not specified.) -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Unable to install "collect" via pip3
> On 20 Dec 2019, at 15:27, Mahmood Naderan via Python-list > wrote: > > Hi > > I can install collect with pip for python2.7 > $ pip install --user collect > Collecting collect > Using cached > https://files.pythonhosted.org/packages/cf/5e/c0f0f51d081665374a2c219ea4ba23fb1e179b70dded96dc16606786d828/collect-0.1.1.tar.gz > Collecting couchdbkit>=0.5.7 (from collect) > Using cached > https://files.pythonhosted.org/packages/a1/13/9e9ff695a385c44f62b4766341b97f2bd8b596962df2a0beabf358468b70/couchdbkit-0.6.5.tar.gz > Collecting restkit>=4.2.2 (from couchdbkit>=0.5.7->collect) > Downloading > https://files.pythonhosted.org/packages/76/b9/d90120add1be718f853c53008cf5b62d74abad1d32bd1e7097dd913ae053/restkit-4.2.2.tar.gz > (1.3MB) >100% || 1.3MB 633kB/s > Collecting http-parser>=0.8.3 (from > restkit>=4.2.2->couchdbkit>=0.5.7->collect) > Downloading > https://files.pythonhosted.org/packages/07/c4/22e3c76c2313c26dd5f84f1205b916ff38ea951aab0c4544b6e2f5920d64/http-parser-0.8.3.tar.gz > (83kB) >100% || 92kB 2.4MB/s > Collecting socketpool>=0.5.3 (from restkit>=4.2.2->couchdbkit>=0.5.7->collect) > Downloading > https://files.pythonhosted.org/packages/d1/39/fae99a735227234ffec389b252c6de2bc7816bf627f56b4c558dc46c85aa/socketpool-0.5.3.tar.gz > Building wheels for collected packages: collect, couchdbkit, restkit, > http-parser, socketpool > Running setup.py bdist_wheel for collect ... done > Stored in directory: > /home/mnaderan/.cache/pip/wheels/b9/7c/7c/b09b334cc0e27b4f63ee9f6f19ca1f3db8672666a7e0f3d9cd > Running setup.py bdist_wheel for couchdbkit ... done > Stored in directory: > /home/mnaderan/.cache/pip/wheels/f6/05/1b/f8f576ef18564bc68ab6e64f405e1263448036208cafb221e0 > Running setup.py bdist_wheel for restkit ... done > Stored in directory: > /home/mnaderan/.cache/pip/wheels/48/c5/32/d0d25fb272791a68c49c26150f332d9b9492d0bc9ea0cdd2c7 > Running setup.py bdist_wheel for http-parser ... done > Stored in directory: > /home/mnaderan/.cache/pip/wheels/22/db/06/cb609a3345e7aa87206de160f00cc6af364650d1139d904a25 > Running setup.py bdist_wheel for socketpool ... done > Stored in directory: > /home/mnaderan/.cache/pip/wheels/93/f6/8c/65924848766618647078cb66b1d964e8b80876536e84517469 > Successfully built collect couchdbkit restkit http-parser socketpool > Installing collected packages: http-parser, socketpool, restkit, couchdbkit, > collect > Successfully installed collect-0.1.1 couchdbkit-0.6.5 http-parser-0.8.3 > restkit-4.2.2 socketpool-0.5.3 > However, pip3 fails with this error > $ pip3 install --user collect > Collecting collect > Using cached > https://files.pythonhosted.org/packages/cf/5e/c0f0f51d081665374a2c219ea4ba23fb1e179b70dded96dc16606786d828/collect-0.1.1.tar.gz > Collecting couchdbkit>=0.5.7 (from collect) > Using cached > https://files.pythonhosted.org/packages/a1/13/9e9ff695a385c44f62b4766341b97f2bd8b596962df2a0beabf358468b70/couchdbkit-0.6.5.tar.gz >Complete output from command python setup.py egg_info: >Traceback (most recent call last): > File "", line 1, in > File "/tmp/pip-build-qf95n0tt/couchdbkit/setup.py", line 25, in >long_description = file( >NameError: name 'file' is not defined My guess is that file is python 2 only. Couchdbkit needs porting to python 3. Barry > > > Command "python setup.py egg_info" failed with error code 1 in > /tmp/pip-build-qf95n0tt/couchdbkit/ > I can not figure out what is the problem. Any way to fix that? > > More info: > $ which python > /usr/bin/python > $ ls -l /usr/bin/python > lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7 > $ which python3 > /usr/bin/python3 > $ ls -l /usr/bin/python3 > lrwxrwxrwx 1 root root 9 Jun 21 2018 /usr/bin/python3 -> python3.6 > > > > Regards, > Mahmood > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: How to extend an object?
On 21/12/19 1:59 am, Stefan Ram wrote: I would like to add a method to a string. This is not possible in Python? It's not possible. Built-in classes can't have methods added to them. You can define your own subclass of str and give it whatever methods you want. But in your case: for s in 'abc'.like( '(.)' ): I would recommend making it a stand-alone function instead, so that you would write for s in like('abc', '(.)'): -- Greg -- https://mail.python.org/mailman/listinfo/python-list
How to combine a group of lists together
I utilized .get_text() + .split() functions and obtain this: spe= ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details'] ['4.7', 'inches'] ['750', 'x', '1334'] ['64', 'GB'] ['2', 'GB'] ['Apple', 'A11', 'Bionic,', 'Hexa-Core,', '(2x', 'Monsoon', '+', '4x', 'Mistral)'] ['5.22', 'ounces'] ['No'] ['No'] ['12', 'MP'] ['7', 'MP'] ['1821', 'mah'] ['No'] ['Yes,', 'on', 'the', 'front'] ['None'] ['iOS', '(Last', 'OS', 'info:', 'IOS', '11)'] ['September', '2017'] They have a shared name 'spe', but no concrete names for each list. If I use spe[0], I can only get all the first element of each list. How can I mix all of these elements into a single list like: ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details','4.7', 'inches'..] Thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: How to combine a group of lists together
On 20Dec2019 21:04, wanghao7...@gmail.com wrote: I utilized .get_text() + .split() functions and obtain this: spe= ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details'] ['4.7', 'inches'] ['750', 'x', '1334'] ['64', 'GB'] ['2', 'GB'] ['Apple', 'A11', 'Bionic,', 'Hexa-Core,', '(2x', 'Monsoon', '+', '4x', 'Mistral)'] ['5.22', 'ounces'] ['No'] ['No'] ['12', 'MP'] ['7', 'MP'] ['1821', 'mah'] ['No'] ['Yes,', 'on', 'the', 'front'] ['None'] ['iOS', '(Last', 'OS', 'info:', 'IOS', '11)'] ['September', '2017'] They have a shared name 'spe', but no concrete names for each list. Does that mean you have a loop, with spe being computed in each loop pass? It would help to see your code. If I use spe[0], I can only get all the first element of each list. Indeed, since that designates the first element. How can I mix all of these elements into a single list like: ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details','4.7', 'inches'..] Have a look at the chain() function from the itertools module. Or at the extend() method of the list class. Either will help you compose a longer list from a collection of individual lists. If you're still stuck, post your code in a followup message and ask sepecific questions about your attempts to build a bigger list from various smaller ones. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How to combine a group of lists together
On 21/12/19 6:04 pm, wanghao7...@gmail.com wrote: I utilized .get_text() + .split() functions and obtain this: spe= ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details'] ['4.7', 'inches'] ['750', 'x', '1334'] ['64', 'GB'] ['2', 'GB'] ['Apple', 'A11', 'Bionic,', 'Hexa-Core,', '(2x', 'Monsoon', '+', '4x', 'Mistral)'] ['5.22', 'ounces'] ['No'] ['No'] ['12', 'MP'] ['7', 'MP'] ['1821', 'mah'] ['No'] ['Yes,', 'on', 'the', 'front'] ['None'] ['iOS', '(Last', 'OS', 'info:', 'IOS', '11)'] ['September', '2017'] They have a shared name 'spe', but no concrete names for each list. If I use spe[0], I can only get all the first element of each list. How can I mix all of these elements into a single list like: ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details','4.7', 'inches'..] What is type( spe )? Please copy-paste code/session into your email msg - the above is NOT Python. Your best solution may be to use a loop (instead of the constant 0, as list-argument). The answer to your specific question is 'in the manual': list.extend() (but a loop will likely still be required...) WebRef: https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: How to extend an object?
On 21/12/19 2:50 pm, Greg Ewing wrote: On 21/12/19 1:59 am, Stefan Ram wrote: I would like to add a method to a string. This is not possible in Python? It's not possible. Built-in classes can't have methods added to them. You can define your own subclass of str and give it whatever methods you want. But in your case: for s in 'abc'.like( '(.)' ): I would recommend making it a stand-alone function instead, so that you would write for s in like('abc', '(.)'): Contrarily: sub-classing str and making like() a method strikes me as the way to go, particularly if the 'abc' disappears into the instance's __init__() - and better still if the '(.)' may also be absorbed as a class/instance attribute, and even more-so if like() is not the only method/stand-alone function required for this category of strings... but... choose a self-documenting name for the class, and choose a better-documenting name for the method. instance = class( "abc" ) ... for s in instance.method(): is clean, self-documenting (or will be once you've done your thing), easier to test (fewer args => easier assured testing). -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: How to combine a group of lists together
thanks for responding! Now I've solved this problem! My former code is: for j in data: spe = j.get_text() l_spe = spe.split() print(l_spe) Then I get the result: ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details'] ['4.7', 'inches'] ['750', 'x', '1334'] ['64', 'GB'] ['2', 'GB'] ['Apple', 'A11', 'Bionic,', 'Hexa-Core,', '(2x', 'Monsoon', '+', '4x', 'Mistral)'] ['5.22', 'ounces'] ['No'] ['No'] ['12', 'MP'] ['7', 'MP'] ['1821', 'mah'] ['No'] ['Yes,', 'on', 'the', 'front'] ['None'] ['iOS', '(Last', 'OS', 'info:', 'IOS', '11)'] ['September', '2017'] Now my code is: for j in data: spe = j.get_text() temp_li.extend(spe.split()) print(temp_li) Then: ['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details', '4.7', 'inches', '750', 'x', '1334', '64', 'GB', '2', 'GB', 'Apple', 'A11', 'Bionic,', 'Hexa-Core,', '(2x', 'Monsoon', '+', '4x', 'Mistral)', '5.22', 'ounces', 'No', 'No', '12', 'MP', '7', 'MP', '1821', 'mah', 'No', 'Yes,', 'on', 'the', 'front', 'None', 'iOS', '(Last', 'OS', 'info:', 'IOS', '11)', 'September', '2017'] Bravo! -- https://mail.python.org/mailman/listinfo/python-list
Re: How to combine a group of lists together
Thanks! Actually, I neglected that my "print" is in the "for", so I get a group of lists. Now I put the "print" out of the iteration then use .extend to append each list I produce. Finally, print and get my ideal result. Haha, I'm too naive, so is my English. -- https://mail.python.org/mailman/listinfo/python-list