mirror of
https://github.com/gmihaila/ml_things.git
synced 2021-10-04 01:29:04 +03:00
add download_url function & clean folders
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
# Python Debugging tool
|
||||
|
||||
To set debugging:
|
||||
|
||||
```python
|
||||
import pdb; pdb.set_trace() #<---------DEBUGGING---------------
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```python
|
||||
def add(a, b):
|
||||
'''
|
||||
This is the test:
|
||||
>>> add(2, 2)
|
||||
5
|
||||
'''
|
||||
import pdb; pdb.set_trace() #<---------DEBUGGING---------------
|
||||
return a + b
|
||||
```
|
||||
@@ -1,36 +0,0 @@
|
||||
# Download file from url inside of Python
|
||||
|
||||
## Imports
|
||||
|
||||
```python
|
||||
import os
|
||||
import requests
|
||||
```
|
||||
|
||||
## Function:
|
||||
|
||||
```python
|
||||
def download_from(url, path):
|
||||
"""Download file from url.
|
||||
Args:
|
||||
url: Url to file.
|
||||
path: Path to save file
|
||||
|
||||
Returns:
|
||||
file_path: path where file is saved.
|
||||
"""
|
||||
file_name = os.path.basename(url)
|
||||
os.makedirs(path) if not os.path.isdir(path) else None
|
||||
file_path = os.path.join(path, file_name)
|
||||
if not os.path.isfile(file_path):
|
||||
response = requests.get(url)
|
||||
file_size = open(file_path, 'wb').write(response.content)
|
||||
file_size = '{0:.2f}MB'.format(file_size / 1024)
|
||||
print("%s %s" % (file_path, file_size))
|
||||
return file_path
|
||||
```
|
||||
|
||||
## Use Function:
|
||||
```python
|
||||
download_from(url="https://raw.githubusercontent.com/gmihaila/machine_learning_things/master/sentiments/positive-words.txt", path="senwst")
|
||||
```
|
||||
@@ -1,21 +0,0 @@
|
||||
# File handling
|
||||
|
||||
Easy and simple read/write file handling.
|
||||
|
||||
## Imports
|
||||
|
||||
```python
|
||||
import io
|
||||
```
|
||||
|
||||
## Write to file
|
||||
|
||||
```python
|
||||
io.open("my_file.txt", mode='w', encoding='utf-8').write("Hello!"))
|
||||
```
|
||||
|
||||
## Read from file
|
||||
|
||||
```python
|
||||
io.open("my_file.txt", mode='r', encoding='utf-8').read()
|
||||
```
|
||||
@@ -1,5 +0,0 @@
|
||||
# Universal Text Tool
|
||||
|
||||
Given any text as input, output any desired features for that text: tokens, sentences, embeddings, transformers embeddings.
|
||||
|
||||
Everything would be as inference model.
|
||||
@@ -7,6 +7,7 @@ __version__ = "0.0.1"
|
||||
# functions imports
|
||||
from .array_functions import (pad_array,
|
||||
batch_array)
|
||||
from .web_related import (download_from)
|
||||
|
||||
# alternative names
|
||||
from .array_functions import batch_array as chunk_array
|
||||
|
||||
43
src/ml_things/web_related.py
Normal file
43
src/ml_things/web_related.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2020 George Mihaila.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Functions related to web applications"""
|
||||
|
||||
import os
|
||||
import requests
|
||||
|
||||
|
||||
def download_from(url, path):
|
||||
"""Download file from url.
|
||||
|
||||
:param url: web path of file.
|
||||
:param path: path to save the file.
|
||||
:return: path where file was saved
|
||||
"""
|
||||
|
||||
# get file name from url
|
||||
file_name = os.path.basename(url)
|
||||
# create directory frm path if it doesn't exist
|
||||
os.makedirs(path) if not os.path.isdir(path) else None
|
||||
# find path where file will be saved
|
||||
file_path = os.path.join(path, file_name)
|
||||
# check if file already exists
|
||||
if not os.path.isfile(file_path):
|
||||
# if files does not exist - download
|
||||
response = requests.get(url)
|
||||
file_size = open(file_path, 'wb').write(response.content)
|
||||
file_size = '{0:.2f}MB'.format(file_size / 1024)
|
||||
# print file details
|
||||
print("%s %s" % (file_path, file_size))
|
||||
return file_path
|
||||
Reference in New Issue
Block a user