Monthly Archives: February 2024

Google Drive API v3 (googleapiclient.errors.HttpError 404 File not found)

If you try to upload a file in a shared drive (in Google Drive) you may get the above error.

This is my experience and solution as of February 2024 using Python 3.10.

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account

from io import BytesIO
from googleapiclient.http import MediaIoBaseUpload
from google.cloud import translate_v2 as translate

## Credentials of service account
credentials = service_account.Credentials.from_service_account_info(service_account_info, scopes=['https://www.googleapis.com/auth/drive'])

## API Client Service
service = build("drive", "v3", credentials=credentials)

# buffer_memory=BytesIO(b"some initial binary data: \x00\x01") # BytesIO() new_body
buffer_memory=BytesIO() # BytesIO() new_body
buffer_memory.write(new_body.encode('utf-8'))

## Prepare the file in memory (you can upload local file too with MediaBase Upload)
media_body = MediaIoBaseUpload(buffer_memory, mimetype='text/html', 
        chunksize=1024*1024, resumable=False)
                body = {
                        'title': file_name,
                        "name": file_name,
                        "mimeType": "application/vnd.google-apps.document",
                        "driveId": "0APee............PVA",
                        "parents": ['0APe.............PVA'],
                    }

## Upload file
returned_fields="id, name, mimeType, size, webViewLink, exportLinks"
upload_response=service.files().create( body=body,                                                            
   media_body=media_body,
   supportsAllDrives=True,
   fields=returned_fields).execute()

## Share the created file with user
user_permission = {
                    "type": "user",
                    "role": "writer",
                    "emailAddress": share_user,
                }
                perm_response = service.permissions().create(
                    fileId=uploaded_file_id,
                    body=user_permission,
                    fields="id"
                ).execute()
   

The important difference between uploading in private Google drive is that you have to use the following parameters for Shared Drives:

supportsAllDrives=True
driveId -> Id of Shared Drive
parents -> Folder or Shared Drive

Mime Types: List of the supported Mime type here

Tip:

If you want to convert the uploaded file to Google Drive native format use the following parameter:

"mimeType": "application/vnd.google-apps.document",

Google documentation: https://developers.google.com/drive/api/guides/manage-uploads

And this article on StackOverflow as well helped to find the solution although some parameters are wrong or deprecated already: https://stackoverflow.com/questions/67622131/google-drive-api-v3-googleapiclient-errors-httperror-404-file-not-found

Python snippets 2024 Q1

How to convert StringIO object to BytesIO object vice versa.

I want to read a string with the io.StringIO and then convert it to an io.BytesIO object and vice versa, how can I do this?

Below is the example source code which can implement python StringIO and BytesIO object converts.

import io
# Convert a StringIO object to BytesIO object.
def stringio_to_bytesio(str):
    
    str_io_object = io.StringIO(str)
    
    str_data = str_io_object.read().encode('utf8')
    
    bytes_io_object = io.BytesIO(str_data)
    
    print(bytes_io_object)  
    print(bytes_io_object.read())
    
# Use io.TextIOWrapper to convert BytesIO object to a string. Then we can build a StringIO object on the string.     
def bytesio_to_stringio(bytes_str):
    
    data = io.BytesIO(bytes_str)
    
    # Create an instance of io.TextIOWrapper class.
    text_wrapper = io.TextIOWrapper(data, encoding='utf-8')
    
    str = text_wrapper.read()
    
    str_io_object = io.StringIO(str)
    print(str_io_object)  
    
    print(str)  
if __name__ == '__main__':
    
    bytes_str = stringio_to_bytesio('I love python')
    
    bytesio_to_stringio(b'hello python')

Source (with more details): https://www.code-learner.com/python-stringio-and-bytesio-example/

Constrictor hitch (knot) – Констриктор възел

Very good video showing how to tie a constrictor knot (hitch).
Много добро видео с обяснение как се връзва възел “констриктор”:

https://www.facebook.com/reel/925672185428132

https://www.facebook.com/reel/1058932998748146

And wikipedia article for reading: https://en.wikipedia.org/wiki/Constrictor_knot

Renault Laguna III 2.0 dci – Service intervals / Интервали на смяна за Рено Лагуна 3 дизел

As there is no official info in the User’s manual of Laguna 3, I found out this recently useful site:

https://www.auto-abc.eu/renault-laguna/v8718-2013/service

I was interested what is the recommended interval (if any) for the timing chain of this model. So the answer is:

Timing chain

There is no manufacturer specified replacement interval for the engine timing chain on Renault Laguna 2013 2.0 diesel (expecting it to last until the engine is overhauled – nice!).

Based on real life experience, the Renault Laguna 2013 2.0 dCi timing chain change interval is approximately 200,000 km as at this mileage it may become overstretched and require replacement, so it is recommended to check the condition of the chain periodically after this mileage is reached and replace the timing chain if it becomes excessively stretched or if unusual engine noises occur.

Serpentine (drive) belt

Renault Laguna 2013 2.0 dCi serpentine or alternator belt replacement interval is 120,000 km.

Engine coolant

Engine coolant (antifreeze) recommended replacement interval for Renault Laguna 2.0 diesel is 90,000 km or 5 years, whichever comes first.

И на български – понеже няма в книжката на колата по-важните интервали за обслужване при последното търсене намерих полезна информация на този сайт (на английски) за моделът дизел 2.0 на Рено Лагуна 3 – https://www.auto-abc.eu/renault-laguna/v8718-2013/service

Та тук хората казват, че няма препоръчителен период и километри през които да се сменя веригата (ангренажна) на този двигател. Препоръчват от опит на 200хил.км. и по-скоро след тези километри да се проверява. Няма години и срок даден!

Други регулярни, но по-редки (от смяна на масла и филтри) смени споменати:

  • ремък на алтернатора на 120хил.км
  • антифриз – на 90хил.км или 5 години, което настъпи първо

My AI links and bookmarks

Good article from Google on different AI topics like processing language, vision and generative models

https://blog.research.google/2023/01/google-research-2022-beyond-language.html#Gener

The interesting one for me was this “in “LOLNerf: Learn from One Look”, we explore the ability to learn a high quality representation from just a single 2-D image.”

Nice, a?

And next is 3D modeling with a phone shown in this video:

3D model with my phone

I created a 3D MODEL with my PHONE!