Why urllib.request.urlretrieve not transfers control to next instruction - python-3.x

In interpreter 'urllib.request.urlretrieve' work great like this:
Python 3.4.3+ (default, Oct 14 2015, 16:03:50)
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>>urllib.request.urlretrieve('https://download.owncloud.org/download/repositories/stable/Ubuntu_15.10/Release.key', 'Release.key')
('Release.key', <http.client.HTTPMessage object at 0x7fa008f76518>)
In this function will not work:
def owncloudI():
print(ok('Installing OwnCloud...\n'))
urllib.request.urlretrieve('https://download.owncloud.org/download/repositories/stable/Ubuntu_15.10/Release.key', 'Release.key') #<---- stuck here
call(['apt-key', 'add', 'Release.key'])
rep = open('/etc/apt/sources.list.d/owncloud.list', 'a')
print('deb http://download.owncloud.org/download/repositories/stable/Ubuntu_15.10/', file=rep)
rep.close()
call(['apt-get', 'update'])
call(['apt-get', 'install', 'owncloud'])
Code launch with sudo and execute in /tmp cat, therefore i think with rights all ok. Maybe someone has solved a similar problem.

Fix it this with
with urllib.request.urlopen('https://download.owncloud.org/download/repositories/stable/Ubuntu_15.10/Release.key') as response, open('resp.key', 'wb') as out_file:
data = response.read()
out_file.write(data)

Related

What does platform.system() and platform.architecture() return on Apple M1 Silicon?

I don't have an M1 Mac to work with, I read that python supports it. What's the return of these functions on m1 Macs?
platform.system()
platform.architecture()
Thanks.
On the actual M1 Mac, the platform module returns the following values:
shuuji3#momo ~ % python3
Python 3.8.2 (default, Dec 21 2020, 15:06:03)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.platform()
'macOS-11.2.3-arm64-arm-64bit'
>>> platform.system()
'Darwin'
>>> platform.architecture()
('64bit', '')
>>> platform.processor()
'arm'
In addition to that, under the Rosetta 2 (Intel mode), the platform module returns the different values like followings:
shuuji3#momo ~ % env /usr/bin/arch -x86_64 /bin/zsh --login
shuuji3#momo ~ % python3
Python 3.8.2 (default, Dec 21 2020, 15:06:04)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.platform()
'macOS-11.2.3-x86_64-i386-64bit'
>>> platform.system()
'Darwin'
>>> platform.architecture()
('64bit', '')
>>> platform.processor()
'i386'
Note: For the first command, I'm following the instruction in the article, How to Run Legacy Command Line Apps on Apple Silicon | Walled Garden Farmers.
We could use these values to distinguish under which mode the current M1 mac runs an application.
I got an M1 pro and the outputs are:
Python 3.8.15 (default, Nov 10 2022, 13:17:42)
[Clang 14.0.6 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>> import platform
>>> platform.platform()
'macOS-10.16-x86_64-i386-64bit'
>>> platform.system()
'Darwin'

LoadImage in podman with python3

I'm trying to load my tar image in podman.
Python 3.7.8 (default, Jun 29 2020, 05:46:05)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import podman
>>> c = podman.Client(uri='unix://var/run/docker.sock') #docker.sock is my mount point for podman socket
>>> c.
c.containers c.images c.pods c.system
>>> c.images.
c.images.build( c.images.get( c.images.list( c.images.search(
c.images.delete_unused( c.images.import_image( c.images.pull(
>>> c.images.
I don't see any load image method.
Is there any method like c.load_image("/tmp/se.tgz")? similar method exists for docker and easily doable.
There is an existing API that can be used like this.
c = podman.Client(uri='unix://var/run/docker.sock')
with c._client() as podman:
results = podman.LoadImage("", "/tmp/se.tgz", False, False)

Trimming the filename using _ and removing characters using python

If my file name is 5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843_20200616T10_50_UTC.wav .. Output to be 5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843.wav . How can we trim the file name using python?
There are several ways to do, for instance
import os
l=os.path.splitext("5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843_20200616T10_50_UTC.wav")
l[0].split("_")[0] + l[1]
I use os.path.splitext to separate the possible extension
Execution with and without '_' and with and without extension :
pi#raspberrypi:~ $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>> def f(s):
... l = os.path.splitext(s)
... return l[0].split("_")[0] + l[1]
...
>>> f("5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843_20200616T10_50_UTC.wav")
'5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843.wav'
>>>
>>> f("5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843.wav")
'5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843.wav'
>>>
>>> f("5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843_20200616T10_50_UTC")
'5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843'
>>>
>>> f("5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843")
'5f0756fa-75bc-4c70-9ba8-fbd1b6a9f843'
>>>

Create tar archive without repetitive root name

I am creating a tar archive with tarfile module in Python. All ok, but I get a gzip file named archive.gz which containa an archive named archive.gz too. How can I make the archive without repetitive name?
here the code:
import tarfile
import os
src_archive = 'C:\\source_archive'
dest_archive = 'C:\\archive.tar.gz'
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
if __name__ == "__main__":
print('creating archive')
make_tarfile(dest_archive,src_archive)
I had a similar issue, what I did was use UUID to randomly generate a string and append that to the end of the file (which requires a unique name)
Another thing you could do is use a database, and append name by index number.
Python 3.7.3 (default, Mar 27 2019, 09:23:15)
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import uuid
>>> src_archive = f'C:\\source_archive-{str(uuid.uuid4()).split("-")[0]}'
>>> print(src_archive)
C:\source_archive-674fce6b

I am unable to get tensorflow to execute

The following is a snippet from my Python 3.5 interpreter. I don't understand why this produces the error (Listed bellow). Any suggestions?
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64
bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, Tensorflow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Then it gives me just a list of internal Errors starting with:
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]

Resources